-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementFrequency.java
More file actions
36 lines (32 loc) · 1.13 KB
/
ElementFrequency.java
File metadata and controls
36 lines (32 loc) · 1.13 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ElementFrequency {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>();
// Initialize the array list with elements
arrList.add(1);
arrList.add(2);
arrList.add(3);
arrList.add(2);
arrList.add(1);
arrList.add(4);
arrList.add(5);
arrList.add(2);
// Create a HashMap to store element frequencies
Map<Integer, Integer> frequencyMap = new HashMap<>();
// Iterate through the ArrayList and count element frequencies
for (int element : arrList) {
if (frequencyMap.containsKey(element)) {
frequencyMap.put(element, frequencyMap.get(element) + 1);
} else {
frequencyMap.put(element, 1);
}
}
// Print the frequency map
System.out.println("Element Frequencies:");
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}