Skip to content

Commit 44b9d44

Browse files
authored
Merge pull request neetcode-gh#1483 from aadil42/patch-19
Create 169-majority-element.js
2 parents b45b46d + e4a441c commit 44b9d44

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

javascript/169-majority-element.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// problem link https://leetcode.com/problems/majority-element
2+
// time complexity O(n)
3+
4+
var majorityElement = function(nums) {
5+
6+
const occuranceOfElement = new Map();
7+
for(let i = 0; i < nums.length; i++) {
8+
if(occuranceOfElement.has(nums[i])) {
9+
let occurance = occuranceOfElement.get(nums[i]);
10+
occuranceOfElement.set(nums[i], occurance+1);
11+
} else {
12+
occuranceOfElement.set(nums[i], 1);
13+
}
14+
}
15+
16+
for(let [key,value] of occuranceOfElement) {
17+
if(value > nums.length / 2) return key;
18+
}
19+
20+
};

0 commit comments

Comments
 (0)