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




  1. package arrayListRemoveduplicateElements;
  2. import java.util.ArrayList;
  3.  
  4. public class RemoveDuplicates {
  5. public static void main(String[] args){
  6.     
  7.     ArrayList<Object> al = new ArrayList<Object>();
  8.     
  9.     al.add("java");
  10.     al.add('a');
  11.     al.add('b');
  12.     al.add('a');
  13.     al.add("java");
  14.     al.add(10.3);
  15.     al.add('c');
  16.     al.add(14);
  17.     al.add("java");
  18.     al.add(12);
  19.     
  20. System.out.println("Before Remove Duplicate elements:"+al);
  21.  
  22. for(int i=0;i<al.size();i++){
  23.  
  24.  for(int j=i+1;j<al.size();j++){
  25.             if(al.get(i).equals(al.get(j))){
  26.                 al.remove(j);
  27.                 j--;
  28.             }
  29.     }
  30.  
  31.  }
  32.  
  33.     System.out.println("After Removing duplicate elements:"+al);
  34.  
  35. }
  36.  
  37. }



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]


arraylist remove duplicates


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Remove duplicates from arraylist without using collections

Select Menu