# 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
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)
}
return tail.next = new Node(data)
}
// function to remove the duplicate numbers
}
while(temp != null) {
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;
}
}
}
console.log("The given linked list is: ")
// calling function to remove duplicate elements
console.log("The Linked list after the removal of duplicate integers is: ")


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
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)
}
return tail.next = new Node(data)
}
// function to remove the duplicate numbers
}
var myset = new Set()
while(temp != null){
if(myset.has(temp.value)){
prev.next = temp.next;
} else {
prev= prev.next;
}
temp = temp.next;
}
}
console.log("The given linked list is: ")
// calling function to remove duplicate elements
console.log("The Linked list after removal of duplicate integers is: ")


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

109 Views 