Skip to content

Commit

Permalink
Update Majority_element.java
Browse files Browse the repository at this point in the history
  • Loading branch information
naruto-o authored Feb 13, 2023
1 parent 9fd375c commit ebc0b98
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Majority_element.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ public int majorityElement(int[] nums) {
return 0;
}
}
// moore voting algorithm
class Solution {
public int majorityElement(int[] nums) {
int count =0;
int element = 0;
for(int i = 0;i<nums.length;i++){
if(count == 0){
element = nums[i];
}
if(nums[i] == element){
count++;
}
else{
count--;
}
}
return element;
}
}

0 comments on commit ebc0b98

Please sign in to comment.