-
Notifications
You must be signed in to change notification settings - Fork 43
/
MajorityElement.java
51 lines (42 loc) · 1.26 KB
/
MajorityElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package LeetCodeJava.Array;
// https://leetcode.com/problems/majority-element/
import java.util.HashMap;
public class MajorityElement {
// V0
public int majorityElement(int[] nums) {
if (nums.length == 1){
return nums[0];
}
int res = 0;
int len = nums.length;
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++){
int cur = nums[i];
if (!map.containsKey(cur)){
map.put(cur, 1);
}else {
map.put(cur, map.get(cur)+1);
}
}
for (int key : map.keySet()){
if (map.get(key) > len / 2){
return key;
}
}
return res;
}
// V1
// https://leetcode.com/problems/majority-element/solutions/3407133/o-1-java-solution-ternary-operators-in-linear-time-beats-100/
public int majorityElement_1(int[] nums) {
int majority_index = 0;
int count = 1;
for (int i=1; i < nums.length; i++){
count += nums[i] == nums[majority_index] ? 1 : -1;
if (count == 0){
majority_index = ++i;
count++;
}
}
return nums[majority_index];
}
}