I want my program to be able to read an array of integer type and count the frequency of each integer in the array. For example, if my array consist of 10 components = {1,1,2,3,1,2,1,1,5,4}, I need my program to be able to say that there are five 1's, two 2's, one 3, one 4, and one 5. I could only go as far as the first occurring integer, something is wrong with my second outer for loop, I think
Here's my code so far:
import java.util.*; public class Yellow { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int numOfElem; System.out.println("Enter how many integers you want to input into the array: "); numOfElem = console.nextInt(); System.out.println(); int[] list = new int[numOfElem]; // int i; int freq = 1; System.out.println("Enter " + numOfElem + " integers: "); for (int i = 0; i < numOfElem; i++) list[i] = console.nextInt(); System.out.println(); for (int i = 0; i < numOfElem; i++) { System.out.print(list[i] + " "); int searchItem = list[i]; for (i = i + 1; i < numOfElem; i++) { if (list[i] == searchItem) freq++; else freq = freq; } System.out.print(" " + freq); System.out.println(); } System.out.println(); } }
Thanks for any help.