Skip to content

Commit b83034b

Browse files
committed
Added majority element - Easy
1 parent ac6de7e commit b83034b

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Time: O(n)
3+
* Space: O(1)
4+
*/
5+
class Solution {
6+
public int majorityElement(int[] nums) {
7+
int count = 0;
8+
Integer candidate = null;
9+
for (int num : nums) {
10+
if (count == 0) {
11+
candidate = num;
12+
}
13+
count += (num == candidate) ? 1 : - 1;
14+
}
15+
return candidate;
16+
}
17+
}

0 commit comments

Comments
 (0)