-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajority_Element.java
More file actions
48 lines (44 loc) · 1.25 KB
/
Majority_Element.java
File metadata and controls
48 lines (44 loc) · 1.25 KB
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
/*Input Format: N = 3, nums[] = {3,2,3}
Result: 3
Explanation: When we just count the occurrences of each number
and compare with half of the size of the array, you will get 3 for the above solution. */
import java.util.*;
public class Majority_Element {
// HashMap Approach
public int majority(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i]) + 1);
} else {
map.put(arr[i], 1);
}
}
int max = 0;
int res = 0;
for (int i = 0; i < arr.length; i++) {
if (map.get(arr[i]) > max) {
max = map.get(arr[i]);
res = arr[i];
}
}
return res;
}
// Moore's Voting Algorithm
public int majority2(int[] arr) {
int n = arr.length;
int count = 0;
int el = 0;
for (int i = 0; i < n; i++) {
if (count == 0) {
count = 1;
el = arr[i];
} else if (el == arr[i]) {
count++;
} else {
count--;
}
}
return el;
}
}