Java Program To Print All the Repeated Numbers with Frequency in an Array
In this tutorial, we will learn how to print all the repeated numbers with frequency in an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: Enter the Array Elements: 2 4 3 5 2 3 4 6 7 8 9 6 5 4
Output: Elements with repeated frequency:
Elements Frequency
2 2
3 2
4 3
5 2
6 2
The above problem can be solved in the following ways:
Let us look at each of these methods separately.
Program 1: Find the Repeated numbers with Frequency in an Array
In this method, we will use Hash Map to print the repeated numbers with frequency in an array. The time and space complexity in this method is O(n).
Algorithm
- Start
- Declare a variable to store the array elements
- Ask the user to initialize the variable.
- Declare an array.
- Ask the user to initialize that array.
- Declare a hashmap for the same.
- Using a for loop check if the map contains duplicate elements.
- If duplicate element is found then increase the index at that element by one.
- If duplicate element is not found then assign it by one.
- Start printing the elements.
- Print the elements only if the count is greater than one.
- Stop.
Below is the code for the same.
The below program demonstrates how to print repeated elements with frequency in an array using Hashmap. The map will save a lot of space and time.
/*Java Proggram to find the repeated elements with Frequency*/
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
int n; //Declare variable for array size
System.out.println("Enter the length of the array");
n=sc.nextInt(); //Ask the user to initialize the size of the array
int a[]=new int[n]; //declare Array
System.out.println("Enter the array elements ");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt(); //Initialize Array elements
}
//Hashmap for array elements
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
if (map.containsKey(a[i]))
{
// if duplicate element in the map then increase the value of element at index by 1
int c = map.get(a[i]);
map.replace(a[i], c + 1);
}
// if not a duplicate element in map than assign it by 1.
else
map.put(a[i], 1);
}
//Print the Repeated Elements with Frequency
System.out.println("Elements Frequency");
for (Map.Entry<Integer, Integer> i : map.entrySet())
{
// print only if count of element is greater than 1.
if (i.getValue() > 1)
System.out.println(" "+ i.getKey() + " "+ i.getValue());
else
continue;
}
}
}
Enter the length of the array 10
Enter the array elements 2 3 1 2 2 3 6 1 8 9
Elements Frequency
1 2
2 3
3 2
Program 2: Find the Repeated Numbers with Frequency in an Array
In this method, we will see how to print the repeated numbers with frequency in an array using another array.
Algorithm
- Start
- Declare a variable to store the array size.
- Ask the user to initialize that variable.
- Declare an array.
- Ask the user to initialize that array.
- Declare a variable max and assign it with Integer.MIN_VALUE.
- Declare another array to store the frequency of all the variables.
- Calculate the frequency of each variable.
- Print the element and its frequency only if itsfrequency is more than 1.
- Display the output.
- Stop
Below is the code for the same.
The below program demonstrates how to find the repeated elements in the array using another another. This new array is used to store the frequency of each element and then only those elements are printed whose frequency is greater than 1.
/*Java Proggram to find the repeated elements with Frequency*/
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
int n; //Declare variable for array size
System.out.println("Enter the length of the array");
n=sc.nextInt(); //Ask the user to initialize the size of the array
int arr[]=new int[n]; //declare Array
System.out.println("Enter the array elements ");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt(); //Initialize Array elements
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
int brr[] = new int[max + 1]; //Declare another Array to store the frequency
for (int i = 0; i < n; i++)
{
// increment in array brr for every integer in arr.
brr[arr[i]]++;
}
System.out.println("Element Frequency ");
for (int i = 0; i <= max; i++) {
// output only if element is more than 1 time in array A.
if (brr[i] > 1)
System.out.println(" " + i + " " + brr[i]);
}
}
}
Enter the length of the array 15
Enter the array elements 2 3 1 2 2 3 6 1 8 9 6 8 3 4 6
Element Frequency
1 2
2 3
3 3
6 3
8 2