forked from dimpeshpanwar/javabasicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityElementII.java
More file actions
48 lines (40 loc) · 1.23 KB
/
majorityElementII.java
File metadata and controls
48 lines (40 loc) · 1.23 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
/*Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Test Case I:
Input: nums = [3,2,3]
Output: [3]
Test Case II:
Input: nums = [1]
Output: [1]
Test Case III:
Input: nums = [1,2]
Output: [1,2] */
import java.util.*;
public class majorityElementII {
private static List<Integer> majorityHelper(int[] nums) {
int dec = nums.length / 3;
List<Integer> res = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
}
for(int k: map.keySet()){
if(map.get(k) > dec){
res.add(k);
}
}
return res;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the size of the array: ");
n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i = 0; i < n; i++){
nums[i] = sc.nextInt();
}
List<Integer> res = majorityHelper(nums);
System.out.println("List of elements appearing more than " + n/3 + " times is: " + res);
}
}