forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path136_Single_Number
46 lines (40 loc) · 1.04 KB
/
136_Single_Number
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
36
37
38
39
40
41
42
43
44
45
46
// https://leetcode.com/problems/single-number/
class Solution {
public int singleNumber(int[] nums) {
int count = 0;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++)
{
if (map.containsKey(nums[i])==false)
map.put(nums[i],1);
else
{
map.put(nums[i],map.get(nums[i])+1);
}
}
for(Map.Entry mapElement: map.entrySet()){
int key = (int)mapElement.getKey();
int val = (int)mapElement.getValue();
if(val!=2)
return key;
}
return 0;
}
}
// https://leetcode.com/problems/single-number/solution/
/*
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> hash_table = new HashMap<>();
for (int i : nums) {
hash_table.put(i, hash_table.getOrDefault(i, 0) + 1);
}
for (int i : nums) {
if (hash_table.get(i) == 1) {
return i;
}
}
return 0;
}
}
*/