Program to calculate frequency of each element in Array
Java Program to calculate Total Number of each element in Array.
In this program,given an array of size ‘n’,Consist of m elements in it. We have to find frequency of each element in the array. i.e how many times a given element occurred in the array.
Array:-> 10,20,30,10,30,40,50,50,10,20 Number frequency(No of Times Element Occurred) 10 3 20 2 30 2 40 1 50 2
From the above example it is now clear what we have to do in this program.
Algorithm/program Explanation
- Start
- Define two arrays
- int array[] new int[100];
- int frequency[]=new int [100];
- Using Scanner Class take the user defined Input to Array .
- for(int i=0;i<size;i++)
- array[i]=sc.nextInt();
- Define int variable i,j,size,count for further use.
- Define two for loop:
- a.for(i=0;i<size;i++)
- count=1;
- b. for(j=i+1;j,size;J++)
- b.1.Check for condition
- if(array[i]==array[j]) then
- count=count+1;
- frequency[j]=0;
- b.2. Check for condition
- if (frequency[i] != 0)
- frequency[i] = count;
- b.1.Check for condition
- a.for(i=0;i<size;i++)
- Using Print Statement print array[i] and frequency[i]
- end
Program to calculate the frequency of each element in Array
/*
Java Program to Calculate Frequency of Occurrence of Each elements in The Array */
import java.util.Scanner;
import java.util.Scanner;
public class FrequencyOfArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int array[] = new int[100];
int frequency[] = new int[100];
int size, i, j, count;
System.out.println("Enter the Size of Array:");
size = sc.nextInt();
System.out.println("Enter the Elements in Array:");
for (i = 0; i < size; i++) {
array[i] = sc.nextInt();
frequency[i] = -1;
}
for (i = 0; i < size; i++) {
count = 1;
for (j = i + 1; j < size; j++) {
if (array[i] == array[j]) {
count = count + 1;
frequency[j] = 0;
}
}
if (frequency[i] != 0) {
frequency[i] = count;
}
}
System.out.println("Frequency of Each Element in an Array are:");
System.out.println("Number|Frequency");
for(i=0;i<size;i++){
if(frequency[i]!=0){
System.out.println(" "+array[i]+ "|" + " " + frequency[i]);
}
}
}
}
Enter the Size of Array:
10
Enter the Elements in Array:
10 20 10 30 40 50 20 30 50 40
Frequency of Each Element in an Array are:
Number|Frequency
10| 2
20| 2
30| 2
40| 2
50| 2
Question Asked in this topic
- Given an integer array calculate frequency of occurrence of each element in the array?
- Write a logic to count the frequency of each number in the array?
- Take user defined array,write a logic to find frequency of occurrence of each element present in the array?
Click Here for more Java Programs
More Relevant topic!
- String Functions Java Program
- Java Program to check whether Strings are anagram or not
- Java Program to Swap two Strings without using temp
- Java Program to Search longest Repeating Sequence
- Program to Search Largest & Smallest Word in a String
- Java Program to Search Duplicate Words in the String
- Program to Count vowels Consonant Used in the Sentence
- Java Program to Count Number of Characters in two Strings
- Java Program to Check String Palindrome
- Program to Convert String into Lowercase & Uppercase
- Java Programs List- Java Programs Examples With Output
- Java Program To Transpose Given Matrix
- Java Program to Print Array in Reverse Order
- Java Program to Multiply two or More Matrices
- Java Program to Add or Sub Two Matrices