Find Duplicate Elements and its Frequency in an Array in Java


In Java, Array is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to detect the elements which are repeating in an array and print its frequency.

An array can contain duplicate values as well. So, the number of times an element is present in an array, that is called frequency of the element in the array.

Let’s see how we can do it by using the Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {2, 3, 5, 4, 3, 1, 3, 2, 1, 2, 2}

After performing the array operation to find frequency of repeated elements we will print “repeated array element” along with “number of times” it has been repeated −

1 --> 2
2 --> 4
3 --> 3

Instance-2

Suppose the original array is {2, 3, 5, 4, 3, 1, 3, 2, 1};

After performing the array operation to find frequency of repeated elements we will print “repeated array element” along with “number of times” it has been repeated −

1 --> 2
2 --> 2
3 --> 3

Instance-3

Suppose the original array is {5, 7, 5, 2, 3, 5, 4, 7}

After performing the array operation to find frequency of repeated elements we will print “repeated array element” along with “number of times” it has been repeated −

5 --> 3
7 --> 2

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Sort the array elements.

  • Step 3 − Initialize the variables.

  • Step 4 − Apply the for loop and set the frequency to 1.

  • Step 5 − Apply another for loop and match the array element with the previous for loop.

  • Step 6 − Print the elements of the array along with its frequency.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

Where ‘array’ refers to the array reference.

To sort an array in Java we have inbuilt sort() method in java.util.Arrays class.

Below refers to the syntax of it −

Arrays.sort();

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array Elements.

  • By Using User Defined Method.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array Elements

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm find the repeated array element with its frequency.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args) {
   
      //Declare and initialize the array elements
      int[] array = { 2, 3, 5, 4, 3, 1, 3, 2, 1, };
      
      //sorting an array
      Arrays.sort(array);
      
      //declaring the variables
      int i,j,frequency;
      System.out.println("These elements are repeated along with its frequency-");
      
      //loop for logic implementation
      for(i=0; i<array.length; i++){
         frequency = 1;
         for(j=i+1; j<array.length; j++){
            if(array[j] == array[i]){
               frequency++;
            } else {
               break;
            }
         }
         i=j-1;
         if(frequency > 1){
         
            //printing the output
            System.out.println(array[i] + " --> " + frequency);
         }
      }
   }
   
}

Output

These elements are repeated along with its frequency-
1 --> 2
2 --> 2
3 --> 3

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as a parameter and inside the method as per the algorithm find the repeated array element with its frequency.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args) {
   
      //Declare and initialize the array elements
      int[] array = { 2, 3, 5, 4, 3, 1, 3, 2, 1, 9, 2, 9, 9, 9, 2 };
      
      //sorting an array
      Arrays.sort(array);
      System.out.println("These elements are repeated along with its frequency-");
      
      //call a user defined method
      duplicate(array);
   }
   
   //user defined method
   static void duplicate(int[] array){
   
      //declared the variables
      int i,j,frequency;
      
      //loop for logic implementation
      for(i=0; i<array.length; i++){
         frequency = 1;
         for(j=i+1; j<array.length; j++){
            if(array[j] == array[i]){
               frequency++;
            } else {
               break;
            }
         }
         i=j-1;
         if(frequency > 1){
            //printing the output
            System.out.println(array[i] + " --> " + frequency);
         }
      }
   }
   
}

Output

These elements are repeated along with its frequency-
1 --> 2
2 --> 4
3 --> 3
9 --> 4

In this article, we explored how to find duplicate elements along with its frequency in an array in Java.

Updated on: 05-Jan-2023

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements