Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Thursday, September 25, 2014

How to remove duplicate of LinkedList ?

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 :


No comments: