Skip to content

Commit 5f5e2df

Browse files
authored
Create 229-majority-element-ii.js
1 parent e8d8120 commit 5f5e2df

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

229-majority-element-ii.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const majorityElement = function(nums) {
6+
const res = []
7+
const hash = {}
8+
const len = nums.length
9+
const limit = Math.floor(len / 3)
10+
nums.forEach(el => {
11+
if(hash.hasOwnProperty(''+el)) {
12+
hash[el] += 1
13+
} else {
14+
hash[el] = 1
15+
}
16+
})
17+
Object.keys(hash).forEach(el => {
18+
if(hash[el] > limit) res.push(+el)
19+
})
20+
21+
return res
22+
};

0 commit comments

Comments
 (0)