Skip to content

Commit 7b97dd3

Browse files
authored
Export majorityElement function
1 parent 60ef2e2 commit 7b97dd3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)