Question: https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
注意和括号问题不同的是,成对出现的数字有可能是交错出现,和左边小括号出现了之后第一个出现的一定是右边小括号不同。
Result: https://app.codility.com/demo/results/trainingA54AMU-7DR/
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Set<Integer> set = new HashSet<Integer>();
for(int num : A) {
if(set.contains(num)) {
set.remove(num);
} else {
set.add(num);
}
}
List<Integer> ret = new ArrayList(set);
return ret.get(0);
}
}
标签:set,Arrays,element,int,num,小括号,array,OddOccurrencesInArray
From: https://www.cnblogs.com/KresnikShi/p/16940570.html