// ArrayList<Integer> list = {3, 6, 5, 3, 3, 6}
Collections.sort(list);
ArrayList<Integer> pairs = new ArrayList<Integer>();
int last = Integer.MIN_VALUE; // some value that will never appear in the list
for (Integer cur : list) {
if (last == cur) {
pairs.add(cur);
last = Integer.MIN_VALUE; // some value that will never appear in the list
} else {
last = cur;
}
}
System.out.println(Arrays.toString(pairs.toArray()));
Будет выводить
[3, 6]
** Редактировать **
Немного лучший алгоритм, модифицирует данный список
// ArrayList<Integer> list = {3, 6, 5, 3, 3, 6}
Collections.sort(list);
int index = 1, last;
while (index < list.size()) {
last = list.remove(index - 1);
if (list.get(index - 1) == last) {
index++;
}
if (index == list.size()) {
list.remove(index - 1);
}
}