Codility - OddOccurrencesInArray

Codility - OddOccurrencesInArray

Codility Task:
OddOccurrencesInArray

Problem Definition:
Find value that occurs in odd(unpaired) number of elements.

Solution:
public int solution(int[] array) {
List<Integer> tempArray = new ArrayList<>(); //temporary array for comparison
for (int number: array) {
if (tempArray.contains(number)) {
tempArray.remove(tempArray.indexOf(number));
} else {
tempArray.add(number);
}
}
return tempArray.get(0); //unpaired element
}
view raw .java hosted with ❤ by GitHub

0 comments:

Post a Comment