Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1336 from julienChemillier/patch-23
Browse files Browse the repository at this point in the history
Add 169 in c language
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents 6f3d7d4 + 9e640cc commit a614b2f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions c/169-Majority-Element.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Given an array nums of size n, return the majority element.
Space: O(1)
Time: O(n)
*/

int majorityElement(int* nums, int numsSize){
int candidate=nums[0];
int count=1;
for (int i=1; i<numsSize; i++) {
if (candidate==nums[i]) {
count++;
} else {
count--;
if (count==0) {
count=1;
candidate=nums[i];
}
}
}
return candidate;
}

0 comments on commit a614b2f

Please sign in to comment.