We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents b45b46d + e4a441c commit 44b9d44Copy full SHA for 44b9d44
javascript/169-majority-element.js
@@ -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