Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1397 from loczek/169-Majority-Element
Browse files Browse the repository at this point in the history
Create: 169-Majority-Element.ts
  • Loading branch information
dissesmac authored Nov 2, 2022
2 parents 21cb5b1 + 8294af9 commit 6654d6b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions typescript/169-Majority-Element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function majorityElement(nums: number[]): number {
let count = 0;
let res = 0;

for (let i = 0; i < nums.length; i++) {
if (count === 0) {
res = nums[i];
}

if (nums[i] === res) {
count += 1;
} else {
count -= 1;
}
}

return res;
}

0 comments on commit 6654d6b

Please sign in to comment.