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 60ef2e2 commit 7b97dd3Copy full SHA for 7b97dd3
src/leetcode/169_Majority-Element/20251106.ts
@@ -0,0 +1,17 @@
1
+export function majorityElement(nums: number[]): number {
2
+ let majority = nums[0];
3
+ let count = 1;
4
+
5
+ for (let i = 1; i < nums.length; i++) {
6
+ if (count === 0) {
7
+ majority = nums[i];
8
+ count = 1;
9
+ } else if (nums[i] === majority) {
10
+ count++
11
+ } else {
12
+ count--
13
+ }
14
15
16
+ return majority;
17
+};
0 commit comments