Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e572779

Browse files
Time: 111 ms (40.32%), Space: 44.7 MB (37.83%) - LeetHub
1 parent 7edb46b commit e572779

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var majorityElement = function(nums) {
6+
let numsCounted = {};
7+
const elements = [];
8+
9+
// Using hashmap to store number and their total count
10+
// and then get the top counted number.
11+
nums.forEach(num => {
12+
if (typeof numsCounted[num] !== 'undefined') {
13+
numsCounted[num]++;
14+
} else {
15+
numsCounted[num] = 1;
16+
}
17+
18+
if (numsCounted[num] > parseInt(nums.length / 3) && !elements.includes(num)) {
19+
elements.push(num);
20+
}
21+
});
22+
23+
return elements;
24+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /