forked from markandey007/hacktoberfest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
majority element 2 leetcode
39 lines (35 loc) · 1.04 KB
/
majority element 2 leetcode
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
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> a = new ArrayList<>();
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++)
{
if(map.containsKey(nums[i]))
{
map.put(nums[i],map.get(nums[i])+1);
}
else
{
map.put(nums[i],1);
}
}
int w=(int)(nums.length/3);
/* Collections<Integer> values=map.values();
for(int i:values)
{
if(i>w)
{
list.add(map.get(i));
}
else
continue;
}*/
for(int key:map.keySet())
{
Integer val=map.get(key);
if(val>w)
a.add(key);
}
return a;
}
}