Write a program that finds the most frequently occurring element in an array
of integers. Use ArrayLists to count the number of occurrences of each element.
At the end print the element and its number of occurrences. If there is more
than one such element, any one of them may be printed.
When the program is done, we will see what needs to be changed to
make it work on an array of Strings.
The starting code is below.
import java.util.ArrayList;
public class MostFrequentElement {
/**
Given an array of ints, the program finds and prints the most
frequently occurring element and its number of occurrences.
If there is more than one such element, any one of them may be
printed.
Assume that the given array contains at least one element.
**/
public static void main(String [] args) {
// in this example 1 is the most frequent element, it appears 7 times:
int [] elements = {1, 3, 4, 1, 5, 2, 3, 6, 6, 6, 4, 1, 2, 6, 2, 3,
1, 2, 1, 5, 5, 1, 1, 5, 4};
// Your code goes
// fill in the appropriate results:
System.out.println("The most frequent element " + " occurs " + " times");
}
}