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.Iterator;
import java.util.List;
public class UniqueDuplicate {
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 k1:l)
{
System.out.println(k1);
}
Iterator<String>it1 = l.iterator();
while (it1.hasNext()) {
String s1 = it1.next();
if(l1.contains(s1))
{
l2.add(s1);
}
else
{
l1.add(s1);
}
}
System.out.println("----------------Duplicate value -----------------------");
for(String k2:l2)
{
System.out.println(k2);
}
System.out.println("----------------Unique value -----------------------");
for(String k3:l1)
{
System.out.println(k3);
}
Iterator<String> it = l1.iterator();
while (it.hasNext()) {
String kk = it.next();
if(l2.contains(kk))
{
l2.add(kk);
}
}
if(l1.containsAll(l2))
{
l1.removeAll(l2);
}
System.out.println("----------------final Duplicate value -----------------------");
for(String k4:l2)
{
System.out.println(k4);
}
System.out.println("----------------final unique value -----------------------");
for(String k5:l1)
{
System.out.println(k5);
}
}
}
o/p:-----------------------------------------
--------List Contains Data----------
govind
ballabh
khan
govind
ballabh
khan
madhubani
bihar
----------------Duplicate value -----------------------
govind
ballabh
khan
----------------Unique value -----------------------
govind
ballabh
khan
madhubani
bihar
----------------final Duplicate value -----------------------
govind
ballabh
khan
govind
ballabh
khan
----------------final unique value -----------------------
madhubani
bihar
No comments:
Post a Comment