Thursday, September 25, 2014

How to remove duplicate of LinkedList ?

6:31 PM

How to remove duplicates of a LinkedList ?

This is one of the most frequently asked java question in oracle interview. There can be n number of ways to do the task, below program demonstrates a simple way of doing this.



import java.util.*;
class RemoveLinkedListDuplicate
{ 
 public static void removeDuplicate()
 {
  LinkedList l=new LinkedList();
  l.add("J");
  l.add("A");
  l.add("V");
  l.add("A");
  l.add("J");
  l.add("A");
  l.add("V");
  l.add("A");
  System.out.println(l);
  LinkedHashSet lhs=new LinkedHashSet(l);
  LinkedList ll=new LinkedList(lhs);
  System.out.println(ll);
 }
 public static void main(String[] args) 
 {
  removeDuplicate();
 }
}

Output of the Above example :


0 comments :

Post a Comment

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy