[Go to site: main page, start]

0% found this document useful (0 votes)
6 views2 pages

Java Program for Element Frequency Count

The Java program counts the frequency of each unique element in an array input by the user. It prompts the user to enter the number of elements and their values, then calculates and displays the frequency of each element. The output presents the elements alongside their respective frequencies in a formatted table.

Uploaded by

Manoranjan Misro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Program for Element Frequency Count

The Java program counts the frequency of each unique element in an array input by the user. It prompts the user to enter the number of elements and their values, then calculates and displays the frequency of each element. The output presents the elements alongside their respective frequencies in a formatted table.

Uploaded by

Manoranjan Misro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;
class TestDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter how many elements: ");
int n = [Link]();
int a[ ] = new int [n]; int [] fr = new int [[Link]];
int visited = -1;
for(int i =0; i<n; i++)
{
[Link]("Enter data for a["+i+"]: ");
a[i]=[Link]();
}
for(int i = 0; i<[Link]; i++)
{
int count = 1;
for(int j = i+1; j<[Link]; j++)
{
if(a[i] == a[j])
{
count++; fr[j] = visited;
}
}
if(fr[i] != visited) fr[i] = count;
}
[Link]("----------------------------");
[Link](" Element | Frequency");
[Link]("----------------------------");
for(int i = 0; i < [Link]; i++)
{
if(fr[i] != visited)
[Link](" " + a[i] + " | " + fr[i]);
}
[Link]("----------------------------");
}
}
Output:
Enter how many elements: 9
Enter data for a[0]: 1
Enter data for a[1]: 2
Enter data for a[2]: 8
Enter data for a[3]: 2
Enter data for a[4]: 2
Enter data for a[5]: 2
Enter data for a[6]: 5
Enter data for a[7]: 1
Enter data for a[8]: 3
----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
5 | 1
3 | 1
----------------------------

You might also like