-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortBasedOnTheFrequency.java
More file actions
46 lines (39 loc) · 1.38 KB
/
Copy pathSortBasedOnTheFrequency.java
File metadata and controls
46 lines (39 loc) · 1.38 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
package prepare;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class SortBasedOnTheFrequency {
public static void main(String[] args) {
System.out.println("Enter the size of the array: ");
try (Scanner sc = new Scanner(System.in)) {
int size = sc.nextInt();
System.out.println("Enter the elements of the array: ");
Integer[] arr = new Integer[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
List<Integer> list = Arrays.asList(arr);
System.out.println(list);
System.out.println("Sorted array based upon the frequency: ");
System.out.println(sortByFrequency(list));
}
}
public static List<Integer> sortByFrequency(List<Integer> list) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
map.put(list.get(i), map.getOrDefault(list.get(i), 0) + 1);
}
Collections.sort(list, (a, b) -> {
int freq1 = map.get(a);
int freq2 = map.get(b);
if (freq1 != freq2) {
return freq2 - freq1;
}
return list.indexOf(a) - list.indexOf(b);
});
return list;
}
}