Created
June 22, 2016 00:42
-
-
Save tiagopereira17/ee9429251f3387377279356c299fa012 to your computer and use it in GitHub Desktop.
A non-empty zero-indexed 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.
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 class OddOccurrencesInArray { | |
public int solution(int[] A) { | |
if(A == null || A.length == 0) { | |
return 0; | |
} | |
if(A.length == 1) { | |
return A[0]; | |
} | |
int result = 0; | |
for(int i = 0; i < A.length; i++) { | |
result ^= A[i]; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment