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.
1 parent 0885e2d commit a984838Copy full SHA for a984838
169-majority-element.js
@@ -16,3 +16,22 @@ const majorityElement = function(nums) {
16
.map(el => +el[0])
17
.sort((a, b) => b - a)[0];
18
};
19
+
20
+// another
21
22
+/**
23
+ * @param {number[]} nums
24
+ * @return {number}
25
+ */
26
+const majorityElement = function(nums) {
27
+ let cnt = 1, candidate = nums[0]
28
+ for(let i = 1, n = nums.length; i < n; i++) {
29
+ if(candidate === nums[i]) cnt++
30
+ else cnt--
31
+ if(cnt === 0) {
32
+ cnt = 1
33
+ candidate = nums[i]
34
+ }
35
36
+ return candidate
37
+};
0 commit comments