Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

169. Majority Element

Posted on 2020-09-01 | Edited on 2021-01-22

LeetCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Time complexity: O(N)
class Solution {
public int majorityElement(int[] nums) {
int maxCntNum=Integer.MIN_VALUE;
Map<Integer,Integer> countMap=new HashMap<>();
for(int num:nums){
int cnt=countMap.getOrDefault(num,0)+1;
if(cnt>nums.length/2){
maxCntNum=num;
}
countMap.put(num,cnt);
}
return maxCntNum;
}
}
<1…222324…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%