JavaScript Program for Removing Duplicates From An Unsorted Linked List


The linked list is a linear data structure that consists of nodes, and each node is stored in memory in a non-contiguous manner. Nodes are connected by storing the address of the next node. We are given a linked list that will contain some integers in a random manner and not in a sorted manner. The task is to find the elements of the linked list which are repeated and we have to remove the duplicated ones. We will see the proper code and explanation.

In this problem, we will keep the first copy of the elements of the linked list and remove the elements which are previously present in the linked list or which are not the first among the repeated set of the same elements.

For example −

Given linked list is 1 -> 5 -> 5 -> 2 -> 7 -> 1 -> 2 -> 6 -> 5 -> 7 -> 7-> null.
Output: 1 -> 5 -> 2 -> 7 -> 6 -> null. 

In the given linked list only 5 elements that are 1, 5, 2, 7, and 6 are unique, and others are similar to them so we will remove the duplicate one.

Example: Naive Approach

In this approach, we will use two loops to traverse over the given list.

// class to create the structure of the nodes 
class Node{
   constructor(data){
      this.value = data;
      this.next = null;
   }
}
// function to print the linked list
function print(head){
   var temp = head;
   if(head == null){
      console.log("The given linked list is empty");
   } else {
      var ans = ""
      while(temp.next != null){
         ans += temp.value;
         ans += " -> "
         temp = temp.next
      }
      ans += temp.value
      ans += " -> null"
   }
   console.log(ans)
}
// function to add data in linked list 
function add(data, head, tail){
   return tail.next = new Node(data)
}
// function to remove the duplicate numbers 
function removeDupli(head){
   if(head == null || head.next == null){
      return head;
      }
      var temp = head.next;
      while(temp != null) {
         var temp2 = head;
         while(temp2 != temp && temp2.value != temp.value){
            temp2 = temp2.next;
         }
         if(temp2 == temp) {
            temp = temp.next;
         } else {
            while(temp2.next != temp) {
               temp2 = temp2.next;
            }
            temp2.next = temp.next;
            temp = temp.next;
         }
      }
   return head;
}
// defining linked list
var head  = new Node(1)
var tail  = head
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(2,head, tail)
tail = add(7,head, tail)
tail = add(1,head, tail)
tail = add(2,head, tail)
tail = add(6,head, tail)
tail = add(5,head, tail)
tail = add(7,head, tail)
tail = add(7,head, tail)
console.log("The given linked list is: ")
print(head)
// calling function to remove duplicate elements 
head = removeDupli(head)
console.log("The Linked list after the removal of duplicate integers is: ")
print(head)

The time complexity of above code is O(N*N) and space complexity is O(1).

Example: Using Hashing

In this approach we will use hash set to find repeated elements

// class to create the structure of the nodes 
class Node{
   constructor(data){
      this.value = data;
      this.next = null;
   }
}
// function to print the linked list
function print(head){
   var temp = head;
   if(head == null){
      console.log("The given linked list is empty");
   } else {
      var ans = ""
      while(temp.next != null){
         ans += temp.value;
         ans += " -> "
         temp = temp.next
      }
      ans += temp.value
      ans += " -> null"
   }
   console.log(ans)
}
// function to add data in linked list 
function add(data, head, tail){
   return tail.next = new Node(data)
}
// function to remove the duplicate numbers 
function removeDupli(head){
   if(head == null || head.next == null){
      return head;
   }
   var temp = head.next;
   var prev = head;
   var myset = new Set()
   myset.add(head.value);
   while(temp != null){
      if(myset.has(temp.value)){
         prev.next = temp.next;
      } else {
         prev= prev.next;
         myset.add(temp.value);
      }
      temp = temp.next;
   }
   return head;
}
// defining linked list
var head  = new Node(1)
var tail  = head
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(2,head, tail)
tail = add(7,head, tail)
tail = add(1,head, tail)
tail = add(2,head, tail)
tail = add(6,head, tail)
tail = add(5,head, tail)
tail = add(7,head, tail)
tail = add(7,head, tail)
console.log("The given linked list is: ")
print(head)
// calling function to remove duplicate elements 
head = removeDupli(head)
console.log("The Linked list after removal of duplicate integers is: ")
print(head)

The time complexity of above code is O(N*log(N)) and space complexity is O(1).

Conclusion

In this tutorial, we have implemented two pointers and hashing techniques to remove the duplicate elements from the given linked list. Time complexity of the Naïve or two pointers approach is quadratic or O(N*N), while the time complexity of the hashing is approx. linear or with the log factor that is O(N*log(N)).

Updated on: 12-Apr-2023

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements