Codility Task:
OddOccurrencesInArray
Problem Definition:
Find value that occurs in odd(unpaired) number of elements.
Solution:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
0 comments:
Post a Comment