In this article we are going to see how to find out the frequency of each element in an array in Java.
Java Program to Find the Frequency of Each Element of an Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find out the frequency of each element in an array.
Method-: Java Program to Find the Frequency of Each Element of an Array By Static Initialization of Array Elements and User Defined Method
Approach:
- Create an array with elements, and another blank array of same size called freq.
- Set all the elements in the blank array to -1 using fill( ) library function.
- Display the array elements to the user.
- Pass both the arrays into an user function frequency ( ) that finds and stores the number of occurrence of elements.
- Use a counter variable to count the number of times the element occurs inside the array.
- Store it in the freq array at same location as the element.
- Print the elements from the main array where the freq is greater than equals to 1 with the frequency.
Program:
import java.util.*; import java.util.Scanner; public class Main { public static void main(String args[]) { // Creating the array int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30,20,30,50,10,50,30,20}; int freq[] = new int[arr.length]; // Sets all elements in the array to -1 Arrays.fill(freq, -1); // Prints the array elements System.out.println("The array elements are "+Arrays.toString(arr)); frequency(arr,freq); } // Function that counts the frequency of elements static void frequency (int arr[], int freq[]) { int count; for(int i = 0; i<arr.length; i++) { // Resets count to 1 after each element count=1; for(int j = i + 1; j<arr.length;j++) { // If another occurence of the current element is found // in the array then increments the counter if(arr[i]==arr[j]) { count++; freq[j] = 0; } } // Stores the frequency of each element if(freq[i]!=0) { freq[i] = count; } } // Prints the elements with their occurences System.out.println("The elements and their occurences are"); for(int i = 0; i<arr.length;i++) { if(freq[i]>=1) System.out.println("Element "+arr[i]+" has occured "+freq[i]+" times."); } } }
Output: The array elements are [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30, 20, 30, 50, 10, 50, 30, 20] The elements and their occurences are Element 12 has occured 1 times. Element 22 has occured 2 times. Element 34 has occured 2 times. Element 54 has occured 2 times. Element 6 has occured 1 times. Element 52 has occured 1 times. Element 8 has occured 1 times. Element 9 has occured 1 times. Element 68 has occured 1 times. Element 10 has occured 2 times. Element 20 has occured 3 times. Element 30 has occured 3 times. Element 50 has occured 2 times.
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.