Write a method named max0ccurrences that accepts a list of integers as a paramet
ID: 3872955 • Letter: W
Question
Write a method named max0ccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the "mode") occurs in the list. Solve this problem using a map as auxiliary storage. If the list is empty, return 8. Do not parameter modify the list passed in as a Type your Java solution code here This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above. 4 Indent Submit 2 Sound F/X 2 Highlighting Clos . Error emitted from internal testing code. This error is occurring when our internal testing code is trying to call your code.If you see this, it may mean that you Your code did not compile. Please read and correct the errors below. have the wrong header for your solution, or a mismatched ) brace. You have a m between data types. The type that was expected ('ArraylistcInteger') doesn't match the type you provided ('Integerl) types: returnValue ArrayList cannot be converted to Integer [ incompatible int = max0ccurrences (par anB); 20 rs 7 8Explanation / Answer
Source Code:
import java.util.*;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class FindFrequency
{
static void findMostFrequentElement(int inputArray[])
{
//Creating HashMap object with elements as keys and their occurrences as values
HashMap<Integer, Integer> elementCountMap = new HashMap<Integer, Integer>();
//Inserting all the elements of inputArray into elementCountMap
for (int i : inputArray)
{
if (elementCountMap.containsKey(i))
{
//If an element is present, incrementing its count by 1
elementCountMap.put(i, elementCountMap.get(i)+1);
}
else
{
//If an element is not present, put that element with 1 as its value
elementCountMap.put(i, 1);
}
}
int element = 0;
int frequency = 1;
//Iterating through elementCountMap to get the most frequent element and its frequency
Set<Entry<Integer, Integer>> entrySet = elementCountMap.entrySet();
for (Entry<Integer, Integer> entry : entrySet)
{
if(entry.getValue() > frequency)
{
element = entry.getKey();
frequency = entry.getValue();
}
}
//Printing the most frequent element in array and its frequency
if(frequency > 1)
{
System.out.println("Input Array : "+Arrays.toString(inputArray));
System.out.println("The most frequent element : "+element);
System.out.println("Its frequency : "+frequency);
System.out.println("========================");
}
else
{
System.out.println("Input Array : "+Arrays.toString(inputArray));
System.out.println("No frequent element. All elements are unique.");
System.out.println("=========================");
}
}
public static void main(String[] args)
{
int n;
Scanner Sc=new Scanner(System.in);
System.out.println("Enter Array Size");
n=Sc.nextInt();
int a[]=new int[n];
System.out.println("Enter Array Elements");
for(int i=0;i<n;i++)
{
a[i]=Sc.nextInt();
}
findMostFrequentElement(a);
}
}
Output:
Enter Array Size
3
Enter Array Elements
1
2
1
Input Array : [1, 2, 1]
The most frequent element : 1
Its frequency : 2
========================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.