Skip to content

Commit 8ae4763

Browse files
authored
Create 169_Majority_element.java
1 parent bc10c9f commit 8ae4763

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/majority-element
2+
3+
4+
// MY APPROACH
5+
6+
class Solution {
7+
public int majorityElement(int[] nums) {
8+
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
9+
int n = nums.length;
10+
int max = (int)Math.floor(n/2);
11+
for(int i=0;i<n;i++){
12+
map.put(nums[i],map.getOrDefault(nums[i],0)+1);
13+
}
14+
for(Map.Entry mape:map.entrySet()){
15+
int key = ((int)mape.getKey());
16+
int val=((int)mape.getValue());
17+
if(val>max)
18+
return key;
19+
}
20+
return 0;
21+
}
22+
}
23+
24+
// https://leetcode.com/articles/majority-element/

0 commit comments

Comments
 (0)