Java Odd Occurrences In Array
import java.util.LinkedHashMap;
import java.util.Map;
public class Solution {
public static Integer oddOccurrencesInArray(int[] a) {
Map<Integer, Integer> count = new LinkedHashMap<>();
for (int value : a) {
if (!count.containsKey(value)) {
count.put(value, 1);
} else {
count.remove(value);
}
}
for (Integer key : count.keySet()) {
return key;
}
return null;
}
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.