Saturday, July 29, 2017

Find duplicates in ArrayList in java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class dupliInList {
 
             public static void main(String[] args) {
  
                   List list=new ArrayList<>();
                  
                          list.add(10);
                          list.add(20);
                          list.add(30);
                          list.add(10);
                          list.add(20);
  
                          List list2=new ArrayList<>();
  
                  for(int i=0;i<=list.size()-1;i++){ 
                       if(!list2.contains(list.get(i))){
                             list2.add(list.get(i));
                       }
                  }
  
                 System.out.println("With duplicate..."+list);
                 System.out.println("Without duplicate..."+list2);
            }
}