Saturday, 21 April 2018


wap to remove duplicate values from existing list (List l) and also create list of duplicate values (l2) and create list of non duplicate values (l1).

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

public class AssignmentCollection {
public static void main(String[] args) {
List<String> l=new ArrayList<String>();
  List<String> l1=new ArrayList<String>();
    List<String> l2=new ArrayList<String>();
    l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("madhubani");
l.add("bihar");
System.out.println("--------List Contains Data----------");
for(String kk:l)
{
System.out.println(kk);
}

Iterator<String> it = l.iterator();
while (it.hasNext()) {
String s1 =  it.next();
int kk = Collections.frequency(l, s1);
System.out.println("frequency of :"+s1+"------"+kk+" times");
if(kk==1)
{
l1.add(s1);
}
else
{
l2.add(s1);
}

}
System.out.println("--------------unique final list---------------");
for(String kt:l1)
{
System.out.println(kt);
}

System.out.println("--------------duplicate final list---------------");
for(String km:l2)
{
System.out.println(km);
}
}
}


o/p:------------------------

--------List Contains Data----------
govind
ballabh
khan
govind
ballabh
khan
madhubani
bihar
frequency of :govind------2 times
frequency of :ballabh------2 times
frequency of :khan------2 times
frequency of :govind------2 times
frequency of :ballabh------2 times
frequency of :khan------2 times
frequency of :madhubani------1 times
frequency of :bihar------1 times
--------------unique final list---------------
madhubani
bihar
--------------duplicate final list---------------
govind
ballabh
khan
govind
ballabh
khan



No comments:

Post a Comment