by Dinesh Thakur Category: array

Algorithm for Removing duplicate elements from Array:

step 1: read y

step 2: create two integer array a[],b[] of size y

step 3: initialize c=0

step 4: repeat through step-6 while c less than a.length

step 5: read a[c]

step 6: c=c+1

step 7: initialize c=0

step 8: repeat through step-11 while c less than a.length

step 9: print a[c]

step 10: print one space

step 11: c=c+1

step 12: initialize t=0

step 13: repeat through step-l8while t less than a.length

step 14: initialize x=t+1

step 15: repeat through step-17 while x less than a.length

step 16: if a[t] equals to a[x] then set a[x]=0

step 17: x=x+1

step 18: t=t+ 1

step 19: set y =0

step 20: initialize c=0

step 21: repeat through step-23 while c less than a.length

step 22: If a[c] not equals to 0 then set b[y]=a[c] and set y=y+1

step 23: c=c+1

step 24: Initialize x=0

step 25: repeat through step-28 while x less than y

step 26: print b[x]

step 27: print one space

step 28: x=x+1

step 29: Exit

Here is the Java Example for removing duplicate elements from Array:

import java.util.Scanner;

public class RemovingDuplicateElements
{
            public static void main(String args[])
      {
            int c,t,x,y;
            Scanner s=new Scanner(System.in);
            System.out.println("Enter the size of array");
            y=s.nextInt();
            int a[]=new int[y];
            int b[]=new int[y];
            System.out.println("Enter The Elements of Array");
            for(c=0;c<a.length;c++)
               {
                 a[c]=s.nextInt();
               }
                 System.out.println("\nSee the elements of array\n");
                 for(c=0;c<a.length;c++)
                    {
                      System.out.print(a[c]+" ");
                    }
                      System.out.print("\n");
                 for(t=0;t<a.length;t++)
             {
                 for(x=t+1;x<a.length;x++)
                    {
                      if(a[t]==a[x])
                       {
                         a[x]=0;
                       }
                    }
                         y=0;
                 for(c=0;c<a.length;c++)
                    {
                        if(a[c]!=0)
                          {
                            b[y]=a[c] ;
                            y++;
                          }
                    }
System.out.println("\nAfter deleting duplicate elements\n");
               for(x=0;x<=y;x++)
                  {
                     System.out.print(b[x]+" ");
                  }
            }
       }
}




About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles