I want to detect duplicate values in a Java array. For example:
int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 };
How could I get the specific duplicated entry and how many times it occurs?
I'll have a Map<Integer, Integer>
where the first integer is the value
of the number that occurs in the array and the second integer is the count
(number of occurrence).
array.length
in a loopmap.containsKey(array[i])
. If there exists a number in a map, increment that number (something like map.put(array[i], map.get(array[i]) + 1)
. Otherwise, create a new entry in a map (e.g map.put(array[i], 1)
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With