-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (28 loc) · 886 Bytes
/
Copy pathSolution.java
File metadata and controls
31 lines (28 loc) · 886 Bytes
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
import java.util.HashMap;
class Solution {
//Time Complexity: O(n)
//Space Complexity: O(n)
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int major = -1;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (!map.containsKey(num)){
map.put(num, 1);
}else {
map.put(num, map.get(num) + 1);
}
if (map.get(num) > nums.length/2){
major = num;
break;
}
}
return major;
}
public static void main(String[] args) {
int[] nums1 = {3, 2, 3};
System.out.println(new Solution().majorityElement(nums1)); //3
int[] nums2 = {2, 2, 1, 1, 1, 2, 2};
System.out.println(new Solution().majorityElement(nums2)); //2
}
}