Created
August 4, 2014 02:34
-
-
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.
This file contains 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
/** | |
* | |
* 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