Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Scanner;
- public class Duplicate {
- public static void main(String[] args) {
- String source = "1 2 2 2 3 3 4 4 4 5 2 2 2";
- m1(source);
- m2(source);
- }
- private static void m1(String source) {
- Scanner in = new Scanner(source);
- Deque<String> not_duplicate = new ArrayDeque<>();
- Deque<String> duplicate = new ArrayDeque<>();
- while (in.hasNextInt()) {
- String current = Integer.toString(in.nextInt());
- if (not_duplicate.isEmpty() || !current.equals(not_duplicate.getLast())) {
- not_duplicate.addLast(current);
- continue;
- }
- if (duplicate.isEmpty() || !current.equals(duplicate.getLast())) {
- duplicate.addLast(current);
- continue;
- }
- }
- in.close();
- System.out.println(String.join(" ", duplicate));
- }
- private static void m2(String source) {
- Scanner in = new Scanner(source);
- String out = "";
- Integer prev = null;
- Integer last_printed = null;
- while (in.hasNextInt()) {
- int current = in.nextInt();
- if (prev == null || current != prev) {
- prev = current;
- continue;
- }
- if (last_printed == null || current != last_printed) {
- if (last_printed != null) {
- out += " ";
- }
- out += current;
- last_printed = current;
- continue;
- }
- }
- in.close();
- System.out.println(out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement