I am trying to find the unique number of occurrences of each value in the array. I am putting all occurrences of an element in the HashMap. I want to check whether any two adjacent values are the same in HashMap.
I have tried using the Iterator interface to iterate but there seems something missing
class Solution {
public boolean uniqueOccurrences(int[] arr) {
boolean uni = true;
HashMap<Integer, Integer> hm = new HashMap<>();
for (int a : arr) {
if (hm.containsKey(a)) {
hm.put(a, hm.get(a) + 1);
} else {
hm.put(a, 1);
}
}
// Iterate over the HashMap entries
Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
while (it.hasNext()) {
if (it.next().getValue() == it.getValue()) {
uni = false;
break;
}
}
return uni;
}
}
New contributor
user23519882 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.