Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created June 22, 2016 00:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagopereira17/ee9429251f3387377279356c299fa012 to your computer and use it in GitHub Desktop.
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.
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