Created
May 6, 2020 15:06
-
-
Save madsunrise/d6fadea1baa3dadc03aa8463a3e932d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int[] array = new int[n]; | |
Map<Integer, Integer> map = new HashMap<>(); | |
for (int i = 0; i < n; ++i) { | |
int a = scanner.nextInt(); | |
array[i] = a; | |
map.put(a, i); | |
} | |
List<Integer> result = new ArrayList<>(); | |
for (int i = 0; i < n; ++i) { | |
int element = array[i]; | |
if (map.get(element) != i) { | |
result.add(element); | |
} | |
} | |
System.out.println(result.size()); | |
System.out.println(joinToString(result)); | |
} | |
private static <T> String joinToString(Iterable<T> iterable) { | |
StringBuilder sb = new StringBuilder(); | |
for (T item : iterable) { | |
sb.append(item.toString()); | |
sb.append(" "); | |
} | |
if (sb.length() > 0) { | |
sb.deleteCharAt(sb.length() - 1); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment