I was assigned a programming problem for homework, and I am at a stand-still. I have searched for hours today trying to find an answer, and it seems like it's never been asked on here. I basically need to find the reverse of the mode of the array. Here is the question I was asked to find the solution to:
LeastFrequent - Output the integer which occurs least frequently along with its occurrence count from a list of 10 integers input from System.in. If multiple integers in the list occur least frequently, output any integer that occurs least frequently. Name your class LeastFrequent. You can assume that all 10 integers are in the range -100 to 100 inclusive.
Here is the code I have so far:
package leastfrequent;
import java.util.*;
public class LeastFrequent
{
private static int[] arr = new int[10];
private static int minValue;
private static int minCount;
public static void leastCommon()
{
for(int i = 0; i < arr.length; i++)
{
int count = 0;
for(int j = 0; j < arr.length; j++)
{
if(arr[j] == arr[i])
{
count++;
}
}
if(count > minCount)
{
minCount = count;
minValue = arr[i];
}
}
}
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
System.out.print("numbers: ");
for(int i = 0; i < arr.length; i++)
{
arr[i] = stdin.nextInt();
}
Arrays.sort(arr);
leastCommon();
System.out.println("least frequent: " + minValue + " occurs " + minCount + " times");
}
}
Basically I figured if I could find the mode, I could reverse that algorithm and find the least common, but that doesn't work because it always reads zero.
Does anyone have any ideas?
Please help!!