-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSingleNumber.java
More file actions
35 lines (33 loc) · 769 Bytes
/
Copy pathSingleNumber.java
File metadata and controls
35 lines (33 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package leetcode;
/**
* @author eko
*
* Given a non-empty 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?
*
* Example 1:
*
* Input: [2,2,1]
* Output: 1
* Example 2:
*
* Input: [4,1,2,1,2]
* Output: 4
*
*/
public class SingleNumber {
public static void main(String[] args) {
int[] nums = {1,2,3,-1,1,2,3,-1,3};
System.out.println(new SingleNumber().singleNumber(nums));
}
public int singleNumber(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
result = result^nums[i];
}
return result;
}
}