import java.util.HashMap;
import java.util.Map;
public class MostFrequent {
public static int findMostFrequentElement(int[] arr) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : arr) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
int mostFrequent = arr[0];
int maxCount = 0;
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
mostFrequent = entry.getKey();
}
}
return mostFrequent;
}
public static void main(String[] args) {
int[] array = {1, 3, 2, 1, 4, 1, 3, 2, 1};
System.out.println("Most frequent element: " + findMostFrequentElement(array));
}
}