How to remove duplicate elements from Array.






 This  java   code  shoe you  two   way  to remove duplicate  elements  in  Java array . In  first
  java   class uses for loop   and  iterate  through the  array  to  remove the duplicate elements
  of array.

   Another   java class uses HashSet   to remove  the   duplicate  element  of the  array .  If
  think   time   complexity  . Better  to use  HashSet  for  removal .
 





import java.util.HashSet;


public class RemoveDuplicatsinArray {


  public void    RemoveDublicatesA()   {
  
  
   String[] strArray = {"JAVA", "Python", "Perl", "Python", "C", "CPP" };
  
         for (int i = 0; i < strArray.length-1; i++)
         {
             for (int j = i+1; j < strArray.length; j++)
             {
                 if( (strArray[i].equals(strArray[j])) && (i != j) )
                 {
                     System.out.println("Duplicate Element is : "+strArray[j]);
                    
                    
                 }
             }
           }
      
  }




 public  static  void       main(String[] args) {
  
System.out.println("=====   Using   For loop===========================");
     

          RemoveDuplicatsinArray a= new RemoveDuplicatsinArray();
  
           a.RemoveDublicatesA();
          
       

                System.out.println("=====   Using  HashSet ===========================");
         
          System.out.println("****Remove Duplicats  using  Collection:*********");
         

                          DuplicatesInArray b=new DuplicatesInArray();
         
              b.RemoveDublicatesB();   
    
 }

}



 class DuplicatesInArray
  { 
     public  void  RemoveDublicatesB()
    {
      

       String[] strArray = {"JAVA", "Python", "Perl", "Python", "C", "CPP" ,"CPP"};

        HashSet set = new HashSet();

        
        for (String arrayElement : strArray)
        {
            if(!set.add(arrayElement))
            {
                System.out.println("Duplicate Element is : "+arrayElement);
            }
        }
        
        
        
        
        System.out.println( "  Arrays Elecments ");
      
          

        for (String arrayElement : strArray)
             {
         

               System.out.println( arrayElement);
         
               set.add(arrayElement);
                
               
                 }
      
            System.out.println("Elements After removing Dublicates:" );
     
  
          for(String arryset:set){
         
           System.out.println( arryset);
             }
          
                   

    }   

}




This    is  program  out put   but  look  the   out put carefully .  As  it  remove 
different  ArrayList.

=====   Using   For loop===========================

Duplicate Element is : Python
=====   Using  HashSet ===========================
****Remove Duplicats  using  Collection:*********
Duplicate Element is : Python
Duplicate Element is : CPP
  Arrays Elecments 
JAVA
Python
Perl
Python
C
CPP
CPP
Elements After removing Dublicates:
JAVA
CPP
C
Perl
Python

How to remove duplicate elements from Array. How to  remove duplicate elements  from Array. Reviewed by Mukesh Jha on 3:40 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.