JAVA PROGRAM TO REMOVE DUPLICATE ELEMENTS FROM THE ARRAY

 

 

There  are  many  approaches  to  remove  duplicate  elements  from  the  array. The  one  of  the  easiest  way  to  remove  duplicate  elements  from  the  array  is    using  HashSet,  which  automatically   remove  duplicate  elements.  

 

 

 

Example : 

 

 

import    java.util.*;

public  class   RemoveDuplicates{

 //Function  to  remove  duplicate elements  from   array

  public   static   void   removedup(int[]  a)  {

   LinkedHashSet<Integer>    hs= new   LinkedHashSet<Integer> ();

 

  //Adding  elements  to  LinkedHashSet

  for(int  i=0;i <a.length;i++){

   hs.add(a[i]);

}

    System.out.print(hs);

   }

 

public  static  void  main(String  args[]) {

int     a[ ]  =  {  3,5,7,9,13,5,17,3};

    removedup(a);

   }

    }

 

   

 

 

 

 

OUTPUT :

 

[ 3,5,7,9,13,17 ]

 

Leave a Comment

Your email address will not be published. Required fields are marked *