# Java program to find the frequency of elements in an array

## Frequency of elements in an array in java

Now we will learn the program to find Frequency of elements in an array in java which is discussed here. Given an array, the task is to find the frequency of all the elements of the array.For example, consider the following array

Input:

arr = {1, 2, 3, 5, 2,  7, 3, 5}

Output:

1 occurs 1 times

2 occurs 2 times

3 occurs 2 times

5 occurs 2 times

7 occurs 1 times  ## Algorithm

• Input the number of elements of an array.
• Input the array elements.
• Create another array to store the frequency of elements.
• Traverse the input array and update the count of the elements in the frequency array.
• Print the frequency array which displays the frequency of all the elements of the array

``````import java.util.Arrays;

import java.util.Scanner;

public class test {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);
int[] arr = new int;
System.out.println("Elements are not of original array");
for (int i = 0;i< arr.length; i++) {

//           calling the  nextInt for appending the element in the arrays
arr[i]=input.nextInt();

}

//Array freq will store frequencies of element
int [] fr = new int [arr.length];
int visited = -1;

for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}

//Displays the frequency of each element present in array
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(arr[i] + " occurs "+ fr[i]+" times ");
}
}
}``````
```output```Elements are not of original array
1
2
3
1
2
4
5
5
1 occurs 2 times
2 occurs 2 times
3 occurs 1 times
4 occurs 1 times
5 occurs 2 times``````
``````
```output```Elements are not of original array
11
22
33
11
22
44
55
55
11 occurs 2 times
22 occurs 2 times
33 occurs 1 times
44 occurs 1 times
55 occurs 2 times```` ````

### 7 comments on “Java program to find the frequency of elements in an array”

• kandukuri

where can I find this code in c? 1
• HelpPrepInsta

Hey Kandukuri, we don’t have this code in C, we’ll work on it asap, it’ll be really helpful if you can provide us with the code, and we’ll post your code on our page 3
• mohd amer 0
• HelpPrepInsta

yeah we are working on that 1
• Rajpriya

There are some errors in the code 0
• HelpPrepInsta

Hey Rajpriya, can you tell us what errors have you found. 3
• public class Main
{
public static void main(String[] args)
{
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1, 5, 7, 3, 5, 9, 8};
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++)
{
int count = 1;
for(int j = i+1; j < arr.length; j++)
{
if(arr[i] == arr[j])
{
count++;
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
System.out.println("—————————————");
System.out.println(" Element | Frequency");
System.out.println("—————————————");
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(" " + arr[i] + " | " + fr[i]);
}
System.out.println("—————————————-");
}
} 6