-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0136SingleNumber.java
More file actions
33 lines (30 loc) · 853 Bytes
/
_0136SingleNumber.java
File metadata and controls
33 lines (30 loc) · 853 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
package com.heatwave.leetcode.problems;
import java.util.Arrays;
public class _0136SingleNumber {
class Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
int count = 0, n = nums.length;
for (int i = 1; i < n; i++) {
if (nums[i - 1] == nums[i]) {
count++;
} else {
if (count == 0) {
return nums[i - 1];
}
count = 0;
}
}
return nums[n - 1];
}
}
class SolutionXor {
public int singleNumber(int[] nums) {
int num = nums[0], n = nums.length;
for (int i = 1; i < n; i++) {
num = num ^ nums[i];
}
return num;
}
}
}