Question: Find the most frequently occurring item in the array. Example: the most frequently occurring item in [1,3,1,3,2,1] is 1. If you are given an empty
Find the most frequently occurring item in the array. Example: the most frequently occurring item in [1,3,1,3,2,1] is 1. If you are given an empty array, you should return null in (Java). You can assume that there is always a single, unique value that appears most frequent unless the array is empty. For instance, you won't be given an array such as [1,1,2,2]. Note: We are using list instead of arrays in Java for Simplicity. Please use predetermined functions & values.
import java.util.HashMap;
public class MF { public static void main(String[] args) { // NOTE: The following input values are used for testing your solution.
// mostFrequent(array1) should return 1. int[] array1 = {1, 3, 1, 3, 2, 1}; // mostFrequent(array2) should return 3. int[] array2 = {3, 3, 1, 3, 2, 1}; // mostFrequent(array3) should return null. int[] array3 = {}; // mostFrequent(array4) should return 0. int[] array4 = {0}; // mostFrequent(array5) should return -1. int[] array5 = {0, -1, 10, 10, -1, 10, -1, -1, -1, 1}; }
// Implement your solution below. public static Integer mostFreqent(int[] givenArray) { Integer maxItem = null; return maxItem; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
