Java Program to find the frequency of each element in the array

Java Program to find the frequency of each element in the array

Program
You can use a loop and a HashMap to find the frequency of each element in an array in Java. A HashMap is a data structure that stores key-value pairs, and it's commonly used for tasks like counting occurrences of elements in a collection. Here's an example of how you could use a HashMap to find the frequency of each element in an array:
import java.util.HashMap;

public class FrequencyCounter 
{
    public static void main(String[] args) 
    {
        int[] array = {1, 2, 3, 2, 3, 1, 3};
        HashMap<Integer, Integer> frequencyMap = new HashMap<>();
            
        // count the frequency of each element in the array
        for (int i : array) 
        {
            if (frequencyMap.containsKey(i)) 
            {
                frequencyMap.put(i, frequencyMap.get(i) + 1);
            } 
            else 
            {
                frequencyMap.put(i, 1);
            }
        }
            
            // print out the frequency of each element
        for (int key : frequencyMap.keySet()) 
        {
            System.out.println("Element " + key + " occurs " + frequencyMap.get(key) + " times.");
        }
    }
}
In this example, the program initializes an integer array with the values {1, 2, 3, 2, 3, 1, 3} and creates an empty HashMap called frequencyMap. The for loop iterates over the elements of the array and checks if each element is already a key in the HashMap. If it is, the program increases the value associated with that key by one. If it isn't, the program adds a new key-value pair to the HashMap with the element as the key and the value 1.

The second for loop iterates over the keys in the HashMap and prints out the frequency of each element in the format "Element X occurs Y times."

It's a simple but efficient way to count element frequency, as HashMap has O(1) complexity for put, get and containsKey operations.

Also, you can use a similar approach using array instead of HashMap to store the frequency, But It will not be as efficient as using HashMap as searching an element in an array has a O(n) complexity.
Output:







More Questions


1 . Fibonacci Series in Java
2 . Prime Number Program in Java
3 . Palindrome Program in Java
4 . Factorial Program in Java
5 . Armstrong Number in Java
6 . How to Generate Random Number in Java
7 . How to Compare Two Objects in Java
8 . How to Print ASCII Value in Java
9 . How to Reverse a Number in Java
10 . Java Program to convert Number to Word
11 . Automorphic Number Program in Java
12 . Peterson Number in Java
13 . Sunny Number in Java
14 . Tech Number in Java
15 . Keith Number in Java
16 . Neon Number in Java
17 . Spy Number in Java
18 . ATM program Java
19 . Emirp Number in Java
20 . Java Program to copy all elements of one array into another array
21 . Java Program to find the frequency of each element in the arrayJ
22 . Java Program to left rotate the elements of an array
23 . Java Program to print the duplicate elements of an array
24 . Java Program to print the elements of an array
25 . Java Program to print the elements of an array in reverse order
26 . Java Program to print the elements of an array present on even position
27 . Java Program to print the elements of an array present on odd position
28 . Java Program to print the largest element in an array
29 . Java Program to print the smallest element in an array
30 . Java Program to print the number of elements present in an array
31 . Java Program to print the sum of all the items of the array
32 . Java Program to right rotate the elements of an array
33 . Java Program to sort the elements of an array in ascending order
34 . Java Program to sort the elements of an array in descending order
35 . Java Program to Find 3rd Largest Number in an array
36 . Java Program to Find 2nd Largest Number in an array
37 . Java Program to Find Largest Number in an array
38 . Java to Program Find 2nd Smallest Number in an array
39 . Java Program to Find Smallest Number in an array
40 . Java Program to Remove Duplicate Element in an array
41 . Java Program to Print Odd and Even Numbers from an array
42 . How to Sort an Array in Java
43 . Write a Java program to print ‘Hello World!’ on screen
44 . Write a Java program to print the sum of two numbers
45 . Write a Java program that takes two numbers and display the product of two numbers
46 . Write a Java program to print the sum, multiply, subtract, divide and remainder of two numbers
47 . Write a Java program that takes five numbers as input to calculate and print the average of the numbers
48 . Write a Java program to swap two variables
49 . Write a Java program to convert a decimal number to binary numbers
50 . Write a Java program to convert a binary number to decimal number
51 . Write a Java program to check whether Java is installed on your computer or not
52 . Write a Java program and compute the sum of the digits of an integer
53 . Write a Java program to compare two numbers
54 . Write a Java program to count the letters, spaces, numbers and other characters of an input string
55 . Write a Java program to print the ascii value of a given character
56 . Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn
57 . Write a Java program to display the system time
58 . Write a Java program to print the odd numbers from 1 to 20
59 . Write a Java program to print the even numbers from 1 to 20
60 . Write a Java program to convert a string to an integer
61 . Write a Java program to convert seconds to hour, minute and seconds
62 . Write a Java program to compute the sum of the first 100 prime numbers
63 . Write a Java program to swap the first and last elements of an array and create a new array
64 . Write a Java program to count the number of even and odd elements in a given array
65 . Write a Java program to compute the square root of an given integer
66 . Write a Java program to check if a positive number is a palindrome or not
67 . Write a Java program to add two numbers without using any arithmetic operators
68 . Write a Java program to add all the digits of a given positive integer
69 . List of Simple Formula Based Java Programs for Practice
70 . Java program to find area of circle
71 . Java Program to find area of rectangle
72 . Java Program to find area of triangle
73 . Java Program to find area of equilateral triangle
74 . Java Program to find area of rhombus
75 . Java Program to find area of parallelogram
76 . Java Program to find area of Prism
77 . Java Program to find volume of sphere
78 . Java Program to find volume of cylinder
79 . Java Program to find volume of cuboid
80 . Java Program to find volume of cone
81 . Java program to find surface area of cuboid
82 . Java program to find surface area of cylinder
83 . Java program to find surface area of cube
84 . Java program to calculate average marks
85 . Java program to check vowel or consonant
86 . Java program to sum of N numbers
87 . Java program to find factorial of any number
88 . Java Program to calculate electricity bill
89 . Java Program To Calculate CGPA Percentage
90 . Java Program to calculate compound interest
91 . Java Program To Calculate Batting Average
92 . Java Pogram to Calculate Commission Percentage
93 . Java Pogram To Find Distance Between Two Points
94 . Java Program To Calculate Power Of Number
95 . List of all conditional programs in Java programming language
96 . Write a Java program to take three numbers from the user and print the greatest number
97 . Write a Java program to find the number of days in a month
98 . Write a Java program to test a number is positive or negative
99 . Write a Java Program to accept number of week’s day (1-7) and print name of the day
100 . Write a Java program that takes a year from user and print whether that year is a leap year or not
101 . Write a Java program to input 5 numbers from keyboard and find their sum and average
102 . Write a program in Java to display the first 5 natural numbers
103 . Write a java program to check vowel or consonant
104 . Write a Java program to display the cube of the number upto given an integer
105 . Write a Java program to display the n terms of odd natural number and their sum
106 . Write a Java program to display the multiplication table of a given integer
107 . Write a Java program that reads an integer and check whether it is negative, zero, or positive
108 . Write a Java program that reads an positive integer and count the number of digits
109 . Write a Java program that accepts three numbers and check All numbers are equal or not
110 . Write a java program that accepts three numbers from the user and check if numbers are in “increasing” or “decreasing” order.
111 . Write a Java program that determines a student’s grade
112 . Write a Java program to create a simple calculator
113 . List of all string programs in Java Program examples
114 . Write a Java program to concatenate two string
115 . Write a Java program to convert all characters in a string to lowercase
116 . Write a Java program to convert all characters in a string to uppercase
117 . Write a Java program to trim a string(remove whitespaces)
118 . Write a Java program to get a substring of a given string between two specified positions
119 . Write a Java program to replace all the ‘d’ characters with ‘f’ characters
120 . Write a java program to get the length of a given string
121 . Write a java program to print current date and time in the specified format
122 . Write a Java program to get the character at the given index within the String
123 . Write a java program to remove a particular character from a string
124 . Write a java program to reverse a String
125 . Write a java program to remove html tags from a string
126 . Write a java program to count total number of lines from a string
127 . List of Array Programs in Java Program examples
128 . Write a Java program to sum values of an array
129 . Write a Java program to find the index of an array element
130 . Write a Java program to calculate the average value of array elements
131 . Write a Java program to test if an array contains a specific value
132 . Write a Java program to find the maximum and minimum value of an array
133 . Write a Java program to insert an element (specific position) into an array
134 . Write a Java program to reverse an array of integer values
135 . Write a Java program to find the common elements between two arrays
136 . Write a Java program to find the duplicate values of an array of integer values
137 . Write a Java program to convert an array to ArrayList
138 . Write a Java program to add two matrices of the same size
139 . Write a Java program to find second largest number from the array
140 . Write a Java program to find second lowest number from the array
141 . Write a Java program to find the number of even and odd integers in a given array of integers
142 . Write a Java program to get the difference between the largest and smallest values in an array of integers
143 . Write a Java program to segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s
144 . Write a Java program to cyclically rotate a given array clockwise by one
145 . java program to print all unique element in an array
146 . Write a Java Program to Sort the Array in an Ascending Order
147 . Write a Java Program to Sort the Array in an Descending Order
148 . Write a Java Program to Sort Names in an Alphabetical Order
149 . Write a Java Program to Display Transpose Matrix
150 . Write a Java Program to Search Key Elements in an Array
151 . Write a Java Program to Accept the Marks of a Student and find Total Marks and Percentage
152 . List of Loop programs in Java Program examples
153 . Write a java program to print numbers from 1 to 10 using loop
154 . Write a java program to calculate the sum of first 10 natural number using loop
155 . Write a Java program to print multiplication table of given number
156 . Write a Java program to find the factorial value of any number entered through the keyboard
157 . Write a Java program that prompts the user to input an integer and then outputs the number with the digits reversed order
158 . Write a Java program that reads a set of integers, and then prints the sum of the even and odd integers using loop
159 . Write a Java program to check whether the number is a prime number or not
160 . Write a Java program to calculate HCF of Two given numbers using loop
161 . Write a Java program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros
162 . Write a Java program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered
163 . Write a Java program to print out all Armstrong numbers between 1 to 600 using loop
164 . Write a java program to count total number of notes in entered amount using loop
165 . Write a Java program to print Fibonacci series of n terms where n is input by user using loop
166 . Write a java program to calculate the sum of following series where n is input by user
167 . List of Method/Function programming exercises in Java
168 . Write a Java method to compute the average of three numbers
169 . Write a Java method to find the smallest number among three numbers
170 . Write a Java method to count all words in a string
171 . Write a Java method to count all vowels in a string
172 . Write a Java method to compute the sum of the digits in an integer
173 . Write a Java method to check whether an year entered by the user is a leap year or not
174 . Write a Java method to calculate the area of a triangle
175 . Write a Java method to find the area of a pentagon
176 . Write a Java method to find number is even number or not
177 . Write a Java method to check numbers is palindrome number or not
178 . Write a Java method to displays prime numbers between 1 to 20
179 . Write a Java method to find GCD and LCM of Two Numbers
180 . Write a Java method to find factorial using recursion in java
181 . All Collection programs in Java Program examples
182 .
183 . Write a Java program to create a new array list, add some elements (string) and print out the collection
184 . Write a Java program to insert an element into the array list at the first position
185 . Write a Java program to remove the third element from a array list
186 . Write a Java program to sort a given array list
187 . Write a Java program to shuffle elements in a array list
188 . Write a Java program to increase the size of an array list
189 . Write a Java program to reverse elements in a array list
190 . Write a Java program to compare two array lists
191 . Write a Java program to swap two elements in an array list
192 . Write a Java program to join two array lists
193 . Write a Java program to empty an array list
194 . List of Math Exercises Programs in java With an Examples
195 . Write a Java program to reverse an integer number
196 . Write a Java program to round a float number to specified decimals
197 . Write a Java program to test if a double number is an integer
198 . Write a Java program to round up the result of integer division
199 . Write a Java program to convert Roman number to an integer number
200 . Write a Java program to convert a float value to absolute value
201 . Write a Java program to accept a float value of number and return a rounded float value