Automation Using Selenium Webdriver
Showing posts with label Remove duplicates from arraylist without using collections. Show all posts
Showing posts with label Remove duplicates from arraylist without using collections. Show all posts

Sunday 20 November 2016

Remove duplicates from arraylist without using collections

1.Write a Java program to remove duplicate elements from an arraylist without using collections (without using set)



  • package arrayListRemoveduplicateElements;
  • import java.util.ArrayList;
  •  
  • public class RemoveDuplicates {
  • public static void main(String[] args){
  •     
  •     ArrayList<Object> al = new ArrayList<Object>();
  •     
  •     al.add("java");
  •     al.add('a');
  •     al.add('b');
  •     al.add('a');
  •     al.add("java");
  •     al.add(10.3);
  •     al.add('c');
  •     al.add(14);
  •     al.add("java");
  •     al.add(12);
  •     
  • System.out.println("Before Remove Duplicate elements:"+al);
  •  
  • for(int i=0;i<al.size();i++){
  •  
  •  for(int j=i+1;j<al.size();j++){
  •             if(al.get(i).equals(al.get(j))){
  •                 al.remove(j);
  •                 j--;
  •             }
  •     }
  •  
  •  }
  •  
  •     System.out.println("After Removing duplicate elements:"+al);
  •  
  • }
  •  
  • }


  • Output:
    1. Before Remove Duplicate elements:[java, a, b, a, java, 10.3, c, 14, java, 12]
    2. After Removing duplicate elements:[java, a, b, 10.3, c, 14, 12]



    2. Write a Java program to remove duplicate elements from an array using Collections (Linkedhashset)

    1. package arrayListRemoveduplicateElements;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashSet;
    5. import java.util.List;
    6.  
    7. public class RemoveDuplicates {
    8.  
    9. public static void main(String[] args){
    10.     
    11.     List<String>  arraylist = new ArrayList<String>();
    12.     
    13.     arraylist.add("instanceofjava");
    14.     arraylist.add("Interview Questions");
    15.     arraylist.add("Interview Programs");
    16.     arraylist.add("java");
    17.     arraylist.add("Collections Interview Questions");
    18.     arraylist.add("instanceofjava");
    19.     arraylist.add("Java Experience Interview Questions");
    20.     
    21.     
    22.     System.out.println("Before Removing duplicate elements:"+arraylist);
    23.     
    24.     HashSet<String> hashset = new HashSet<String>();
    25.     
    26.     /* Adding ArrayList elements to the HashSet
    27.      * in order to remove the duplicate elements and 
    28.      * to preserve the insertion order.
    29.      */
    30.     hashset.addAll(arraylist);
    31.  
    32.     // Removing ArrayList elements
    33.     arraylist.clear();
    34.  
    35.     // Adding LinkedHashSet elements to the ArrayList
    36.     arraylist.addAll(hashset );
    37.  
    38.     System.out.println("After Removing duplicate elements:"+arraylist);
    39.  
    40. }
    41.  
    42. }



    Output:
      



    1. Before Removing duplicate elements:[instanceofjava, Interview Questions, Interview
    2. Programs, java, Collections Interview Questions, instanceofjava, Java Experience Interview
    3. Questions]
    4. After Removing duplicate elements:[java, Collections Interview Questions, Java Experience
    5. Interview Questions, Interview Questions, instanceofjava, Interview Programs]