Java program to find duplicate elements in an Array

Today we shall learn how to detect the repeated digits in a given array.The main logic behind this is we need to check every number with other numbers.That is first we need to find the length of the given array and then check it.
Group of integer number which is within the limit of the datatype used.
First pick the starting number compare it with other numbers.Next,pick second number and check it with other numbers.If the number picked is present other places too store it in any temporary array and display all the redundant numbers at the end.
It should display all the redundant digits in the given array.
We shall write the code depending on the above stated inputs,explanation and output.

  import java.util.*;
class checking
  int[] temp = new int[20];
     int k=0;
  void check(int n[],int len)
      for(int i=0;i<len;i++)
         for(int j=i+1;j<len;j++)
                   int checkbit=checktemp(n[i]);//Invokes checktemp func to find whether number is already present in array.
                      }//if close
                 } //If close 
            } //For loop of j closed
        } // For loop of i closed  
  int checktemp(int x)
       for(int i=0;i<k;i++)
            return -1;
          }//for loop close
                return 1;
    }//function close
    void display(int n[],int len)
         System.out.println("Given array is:");
            for(int i=0;i<len;i++)
              System.out.print(n[i]+" ");
          System.out.println("\nThere are no redundant elements in the array:");
             System.out.println("\nRedundant numbers in the given array is:");
             for(int i=0;i<k;i++)
             System.out.print(temp[i] + " "); 
class repeated
  public static void main(String arg[])
      Scanner in = new Scanner(;
      System.out.println("Enter the lenght of the array:");
      int len=in.nextInt();
      System.out.println("Enter the data of the Array:");
      int[] n = new int[len];
      for(int i=0;i<len;i++)
         n[i] = in.nextInt();
      checking ob = new checking();
      }//main function close
}// class close

1. In the above program,in the main method we asked the length of the array and then the elements present in the array.
2. Then we declare an object for the class checking and invoked the method check which uses the array,length of the array as parameters.
3. In it we check every number with other number present in the array to find the redundant elements in the arrays.
4. Before adding the number to the temp we need to check whether number is present in the temp or not.For that we invoked the method checktemp which returns -1 if already present,returns 1 if it is not present.
5. Then we called the display method to display the redundant elements.

The output of the above program is

You can get the download the code.


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , . Bookmark the permalink.

1 Response to Java program to find duplicate elements in an Array

  1. Pingback: java program to find redundant digits in a number | letusprogram….!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s