Find the Frequency of Each Element in the Array in Java
We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue. The frequency of each element is calculated by iterating through the array, and it is then stored in another array called fr.
Example
Input : arr [ ] = [1,2,4,3,3,2,1]
Output:
Element Frequency
- 2
- 2
- 2
- 1
Method 1
By using Loops
Algorithm
- STEP 1: BEGIN
- Initiate array[] = [1, 2, 7, 3, 2, 2, 2, 1] .
- Create a frequency [ ] of array[] length in step three.
- SET vis = -1 in step four.
- for (i=0; iarr.length; i++)
- SET count is 1.
- for (i=0;i<frequency.length;i++)
- PRINT array [i] and frequenct [i] if (frequency [i]!=visited)
FrequencyOfElements.java
import java.util.*;
public class FrequencyOfElements {
// Main section of the program where execution begins
public static void main (String[] s) {
// creating object for the scanner class
Scanner sc=new Scanner (System.in);
System.out.println(" Enter array size ");
// enter the size of the array
int n=sc.nextInt();
// array declaration
int array[] = new int [n];
System.out.println(" Enter array elements ");
// enter the array elements
// array initialisation
for(int i=0;i<n;i++)
{
array[i]=sc.nextInt();
}
// printing the array elements
System.out.println(" The array elements are ");
for(int i=0;i<n;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
//Array frequency will store frequencies of an element
int [] frequency = new int [array.length];
int vis = -1;
for(int j = 0; j < array.length; j++){
int count = 1;
// running the for loop from j+1 index to end of the array
for(int i = j+1; i < array.length; i++){
// if 2 elements are equal then count cariable will be incremented
if(array[i] == array[j]){
count++;
//To avoid counting same element again
frequency[i] = vis;
}
}
// if the element is not visited then only count will be updated in the frequency array
if(frequency[j] != vis)
frequency[j] = count;
}
//Displays the frequency of each element present in array
System.out.println(" - -- --- --- --- -----------");
System.out.println(" Elements | Frequency");
// printing the frequencies of array Elements with respect to their array elements
for(int j = 0; j < frequency.length; j++){
if(frequency[j] != vis)
System.out.println(" " + array[j] + " | " + frequency[j]);
}
}}
Output
Enter array size
5
Enter array elements
1 2 1 3 3
The array elements are
1 2 1 3 3
-----------------------
Elements | Frequency
1 | 2
2 | 1
3 | 2
Method 2
FrequencyOfElements2.java
By Using Hashing
import java.util.*;
class FrequencyOfElements2
{
static void Freqz (int arr[], int n)
{
Map<Integer, Integer> m1 = new HashMap < > () ;
// Go through the elements of an array and
for (int i = 0; i < n; i++)
{
if (m1.containsKey(arr[i]))
{
m1.put(arr[i], m1.get(arr[i]) + 1);
}
else
{
m1.put(arr[i], 1);
}
}
// Traversing through map and printing the frequencies
for (Map.Entry<Integer, Integer> traversal : m1.entrySet())
{
System.out.println(traversal.getKey() + " " + traversal.getValue());
}
}
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter size of the array ");
int 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();
}
Freqz(a, n);
}
}
Output
Enter size of the array
8
Enter array elements
12 12 32 44 44 10 10 67
32 1
67 1
10 2
12 2
44 2
By using HashMap
FrequencyOfElements3.java
import java.io.*;
import java.util.*;
class FrequencyOfElements3
{
static void Freqz(int arr[], int size)
{
HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer> () ;
for (int j=0;j<size;j++) {
if (h1.containsKey(arr[j])) {
h1.put(arr[j], h1.get(arr[j]) + 1);
}
else {
h1.put(arr[j], 1);
}
}
// Printing the freqMap
for (Map.Entry e : h1.entrySet()) {
System.out.println(e.getKey() + " " + e.getValue());
}
}
// Driver Code
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter size of the array ");
int 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();
}
Freqz(a, n);
}
}
Output:
Enter size of the array
5
Enter array elements
10 20 10 30 20
20 2
10 2
30 1