Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
//Given an array of integers, every element appears twice except for one.
//Find that single one.
//
//Note:
//Your algorithm should have a linear runtime complexity.
//Could you implement it without using extra memory?
public class SingleNumber {
public static int singleNumber(int[] A) {
int num = A[0];
for (int i = 1; i < A.length; i++) {
num ^= A[i];
}
return num;
}
public static void main(String[] args) {
int[] a = {1,3,4,1,2,4,3,2,9};
int num = SingleNumber.singleNumber(a);
System.out.println(num);
}
}
Submission Details
15 / 15 test cases passed.
| Status: Accepted |
Runtime:
420 ms
|
Submitted:
0 minutes ago
|