Java Program to Find Total Number of Duplicate Numbers in an Array

In the previous article, we have seen Java Program to Print an Array in Reverse Order

In this article we are going to find the duplicate numbers present inside an array in Java.

Java Program to Find Total Number of Duplicate Numbers in an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to print the duplicate elements of an array.

Method-1: Java Program to Print All the Duplicate Elements of an Array By Static Initialization of Array Elements

Approach:

  • Create an array with elements, and another blank array of same size called freq.
  • Set all the elements in the blank array to -1 using fill( ) library function.
  • Display the array elements to the user.
  • Pass both the arrays into an user function unique( ) that finds and stores the number of occurrence of elements.
  • Use a counter variable to count the number of times the element occurs inside the array.
  • Store it in the freq array at same location as the element.
  • Print the elements from the main array where the freq is greater 1.

Program:

import java.util.*;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68};
        int freq[] = new int[arr.length];
        // Sets all elements in the array to -1
        Arrays.fill(freq, -1);
        // Prints the array elements
        System.out.println("The array elements are : "+Arrays.toString(arr));
        
        unique(arr,freq);

    }
    
    // Function that counts the frequency of elements 
    // and prints duplicate elements
    static void unique(int arr[], int freq[])
    {
        int count;

        for(int i = 0; i<arr.length; i++)
        {
            // Resets count to 1 after each element
            count=1;
            for(int j = i + 1; j<arr.length;j++)
            {
                // If another occurence of the current element is found 
                // in the array then increments the counter
                if(arr[i]==arr[j])
                {
                    count++;
                    freq[j] = 0;
                }
            }
            // Stores the frequency of each element
            if(freq[i]!=0)
            {
                freq[i] = count;
            }
        }
        // Prints the duplicate elements
        System.out.print("The duplicate elements in the array are : ");
        for(int i = 0; i<arr.length;i++)
        {
            if(freq[i]>1)
                System.out.print(arr[i]+" ");
        }
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68]
The duplicate elements in the array are : 22 34 54

Method-2: Java Program to Print All the Duplicate Elements of an Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size input and array elements input from the user and create an array.
  • Create another blank array of same size called freq.
  • Set all the elements in the blank array to -1 using fill( ) library function.
  • Display the array elements to the user.
  • Pass both the arrays into an user function unique( ) that finds and stores the number of occurrence of elements.
  • Use a counter variable to count the number of times the element occurs inside the array.
  • Store it in the freq array at same location as the element.
  • Print the elements from the main array where the freq is greater 1.

Program:

import java.util.*;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        
        // Taking size as input from the user
        System.out.println("Enter the array size :");
        int size = scan.nextInt();
        
        // Creating the array
        int arr[] = new int[size];
        
        // Entering the array elements
        System.out.println("Enter array elements : ");
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }
        
        int freq[] = new int[arr.length];
        
        // Sets all elements in the array to -1
        Arrays.fill(freq, -1);
        
        // Prints the array elements
        System.out.println("The array elements are : "+Arrays.toString(arr));
        
        unique(arr,freq);

    }
    
    // Function that counts the frequency of elements 
    // and prints duplicate elements
    static void unique(int arr[], int freq[])
    {
        int count;

        for(int i = 0; i<arr.length; i++)
        {
            // Resets count to 1 after each element
            count=1;
            for(int j = i + 1; j<arr.length;j++)
            {
                // If another occurence of the current element is found 
                // in the array then increments the counter
                if(arr[i]==arr[j])
                {
                    count++;
                    freq[j] = 0;
                }
            }
            // Stores the frequency of each element
            if(freq[i]!=0)
            {
                freq[i] = count;
            }
        }
        
        // Prints the duplicate elements
        System.out.print("The duplicate elements in the array are: ");
        for(int i = 0; i<arr.length;i++)
        {
            if(freq[i]>1)
                System.out.print(arr[i]+" ");
        }
    }
}
Output:

Enter the array size :
Enter array elements : 
The array elements are : [2, 2, 3, 1, 4]
The duplicate elements in the array are: 2

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: