forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpintKMostFreq.java
More file actions
158 lines (124 loc) · 3.87 KB
/
Copy pathpintKMostFreq.java
File metadata and controls
158 lines (124 loc) · 3.87 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
class Pair implements Comparable<Pair> {
int number;
int frequency;
public Pair(int number, int frequency) {
this.number = number;
this.frequency = frequency;
}
@Override
public int compareTo(Pair o) {
if (this.frequency == o.frequency) {
return Integer.compare(o.number, this.number);
}
return Integer.compare(o.frequency, this.frequency);
}
}
public class pintKMostFreq {
/*
* Create a Map Integer vs Integer Store the element in this map as per their
* freq. Create a list Iterate over the keyset Of Map create the pair key vs
* val. add it in the arrayList;
*
* Sort the list
*
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// get the number of integers from input
int n = in.nextInt();
// store all the input integers to the array
int array[] = new int[n];
for (int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
// get the value of k from input
int k = in.nextInt();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
map.put(array[i], map.get(array[i]) + 1);
} else {
map.put(array[i], 1);
}
}
ArrayList<Pair> list = new ArrayList<Pair>();
for (Integer key : map.keySet()) {
int val = map.get(key);
Pair p;
p = new Pair(key, val);
list.add(p);
}
// Sort the list in desc.
Collections.sort(list);
for (int i = 0; i < k; i++) {
System.out.print(list.get(i).number + " ");
}
}
}
// import java.util.*;
// public class Source {
// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// int n = in.nextInt();
// int array[] = new int[n];
// for (int i = 0; i < n; i++)
// {
// array[i] = in.nextInt();
// }
// int k = in.nextInt();
// HashMap<Integer,Integer> map=new HashMap<>();
// for(int i=0;i<n;i++)
// {
// // if(!map.containsKey(array[i]))
// // {
// // map.put(array[i],1);
// // }
// // else{
// // map.put(array[i],map.get(array[i])+1);
// // }
// map.put(array[i],map.getOrDefault(array[i],0)+1);
// }
// //freq map created
// PriorityQueue<Map.Entry<Integer,Integer>> pq=new PriorityQueue<>(
// new Comparator<Map.Entry<Integer,Integer>>(){
// public int compare(Map.Entry<Integer,Integer> a,Map.Entry<Integer,Integer> b)
// {
// if(a.getValue()==b.getValue())
// {
// return b.getKey()-a.getKey();//in descending order
// }
// else
// {
// return b.getValue()-a.getValue();
// }
// }
// });//PQ Sorted According to the Values(in descending order)
// for(Map.Entry<Integer,Integer> entry: map.entrySet())
// {
// pq.add(entry);
// }
// for(int i = 0;i<k;i++)
// {
// System.out.print(pq.poll().getKey() + " ");
// }
// }
// }
// Print k Most Frequent Numbers
// You will be given an array of integers and an integer k. You have to print
// the k most frequently occurring integers in the given array of integers. The
// frequency of an integer in the array is nothing but the number of times it
// appeared in the array.
// Note:
// The integer with higher frequency should be printed first.
// If two integers have the same frequency, then the larger integer of the two
// should be printed first.
// Example:
// Input:
// 1 2 3 2 2 3 4 1 5
// 4
// Output:
// 2 3 1 5