Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pavelnganpi/7cb117f623b7739f03c5 to your computer and use it in GitHub Desktop.
Save pavelnganpi/7cb117f623b7739f03c5 to your computer and use it in GitHub Desktop.
Given an array of positive integers. All numbers occur even number of * times except one number which occurs odd number of times. Find the number * in O(n) time & constant space.
/**
*
* Given an array of positive integers. All numbers occur even number of
* times except one number which occurs odd number of times. Find the number
* in O(n) time & constant space.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3
* @author paveynganpi
*
*/
public class NumberAppearingOddNumberOfTimesInArray {
public static void main(String[]args){
System.out.println(1^2);
System.out.println( "i res arr");
int arr[] = {1, 2, 3, 2, 3, 1, 3};
int res = 0;
for(int i =0;i<arr.length;i++){
res = res^arr[i];
System.out.println(i+ " " + res + " " + arr[i]);
}
System.out.println(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment