Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1483 from aadil42/patch-19
Browse files Browse the repository at this point in the history
Create 169-majority-element.js
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
2 parents b45b46d + e4a441c commit 44b9d44
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions javascript/169-majority-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// problem link https://leetcode.com/problems/majority-element
// time complexity O(n)

var majorityElement = function(nums) {

const occuranceOfElement = new Map();
for(let i = 0; i < nums.length; i++) {
if(occuranceOfElement.has(nums[i])) {
let occurance = occuranceOfElement.get(nums[i]);
occuranceOfElement.set(nums[i], occurance+1);
} else {
occuranceOfElement.set(nums[i], 1);
}
}

for(let [key,value] of occuranceOfElement) {
if(value > nums.length / 2) return key;
}

};

0 comments on commit 44b9d44

Please sign in to comment.