For example my list contains {4, 6, 6, 7, 7, 8} and I want final result = {6, 6, 7, 7}
One way is to loop through the list and eliminate unique values (4, 8 in this
I like answer Java 8, Streams to find the duplicate elements. Solution return only unique duplicates.
Integer[] numbers = new Integer[] { 1, 2, 1, 3, 4, 4 };
Set allItems = new HashSet<>();
Set duplicates = Arrays.stream(numbers)
.filter(n -> !allItems.add(n)) //Set.add() returns false if the item was already in the set.
.collect(Collectors.toSet());
System.out.println(duplicates); // [1, 4]