I have an array as follows:
int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18};
What I wanted to do was find duplicate values and print them.
So my way of doing this was to convert to ArrayList, then to Setand use streamto Set.
ArrayList<Integer> list = new ArrayList<>(array.length);
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
Set<Integer> dup = new HashSet<>(list);
Then I used streamto scroll it and print the values with Collections.frequency.
dup.stream().forEach((key) -> {
System.out.println(key + ": " + Collections.frequency(list, key));
});
Which, of course, prints them all, even if there is only one counter.
I thought to add in if(key > 1), but it is a value that I do not want to use.
How can I get the value in this instance to print only where value > 2.
I could add:
int check = Collections.frequency(list, key);
if (check > 1) {
but it duplicates Collections.frequency(list, key)in streamand is pretty ugly.