July 2017
Beginner to intermediate
715 pages
17h 3m
English
The second approach uses HashMap. First, we create ArrayList to hold possible modes, as in the previous example. We also create our HashMap and a variable to hold the mode:
ArrayList<Double> modes = new ArrayList<Double>(); HashMap<Double, Integer> modeMap = new HashMap<Double, Integer>(); int maxMode = 0;
Next, we loop through our testData array and count the number of occurrences of each value in the array. We then add the count of each value and the value itself to the HashMap. If the count for the value is larger than our maxMode variable, we set maxMode to our new largest number:
for (double value : testData) { int modeCnt = 0; if (modeMap.containsKey(value)) { modeCnt = modeMap.get(value) ...Read now
Unlock full access