Skip to content

Commit 3ac9dfe

Browse files
committed
issue #9 136-done
1 parent ebc9304 commit 3ac9dfe

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcodeEasyLevel;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class _0136SingleNumber {
8+
public static int singleNumber(int[] nums) {
9+
//sol1 => Runtime 1 ms Memory 47.9 MB
10+
// 비트 연산자(^) : 대응되는 비트가 서로 같으면 0을, 다르면 1을 반환함. (비트 XOR 연산)
11+
// http://www.tcpschool.com/c/c_operator_bitwise
12+
/*
13+
int val=0;
14+
for(int i=0;i<nums.length;i++){
15+
val^=nums[i];
16+
}
17+
return val;
18+
*/
19+
20+
//sol2 => Runtime 10 ms Memory 40 MB
21+
// HashSet : contains()을 통해 있으면 remove, 없으면 add해서 남은
22+
// https://www.w3schools.com/java/java_hashset.asp
23+
24+
Set<Integer> box = new HashSet<>();
25+
for (int i = 0; i < nums.length; i++) {
26+
if (box.contains(nums[i])) {
27+
box.remove(nums[i]);
28+
} else {
29+
box.add(nums[i]);
30+
}
31+
}
32+
return box.stream().findFirst().get();
33+
}
34+
35+
public static void main(String[] args) {
36+
int[] nums =// {2, 2, 1};
37+
//{4,1,2,1,2};
38+
//{1};
39+
//{-1,-1,-2}; //=> -2
40+
//{1,3,1,-1,3};
41+
{-336,513,-560,-481,-174,101,-997,40,-527,-784,-283,-336,513,-560,-481,-174,101,-997,40,-527,-784,-283,354}; //=> 354
42+
System.out.println(singleNumber(nums));
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)