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.
1.INPUTS:
Group of integer number which is within the limit of the datatype used.
2.Explanation:
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.
3.Output:
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++)
          {
              if(n[i]==n[j])
                {
                   int checkbit=checktemp(n[i]);//Invokes checktemp func to find whether number is already present in array.
                    if(checkbit==1)
                     {
                         temp[k]=n[i];
                          k++;
                      }//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++)
        {
           if(temp[i]==x)
            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]+" ");
         if(k==0)
          System.out.println("\nThere are no redundant elements in the array:");
         else
          {
             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.in);
      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();
      ob.check(n,len);
      ob.display(n,len);
      }//main function close
}// class close

Explanation:
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

repeated
You can get the download the code.

Advertisement

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:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s