We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bc10c9f commit 8ae4763Copy full SHA for 8ae4763
Leet Code/Practice/169_Majority_element.java
@@ -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