Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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 ones.
Method 1: Naive Approach (Two Loops)
In this approach, we use two loops to traverse over the given list. For each node, we check if any previous node has the same value and remove duplicates accordingly.
// 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");
return;
}
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 given linked list is: 1 -> 5 -> 5 -> 2 -> 7 -> 1 -> 2 -> 6 -> 5 -> 7 -> 7 -> null The Linked list after the removal of duplicate integers is: 1 -> 5 -> 2 -> 7 -> 6 -> null
The time complexity of the above code is O(N²) and space complexity is O(1).
Method 2: Using Hashing (Set)
In this approach, we use a hash set to track visited elements. This provides better time complexity by avoiding nested loops.
// 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");
return;
}
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 using Set
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 given linked list is: 1 -> 5 -> 5 -> 2 -> 7 -> 1 -> 2 -> 6 -> 5 -> 7 -> 7 -> null The Linked list after removal of duplicate integers is: 1 -> 5 -> 2 -> 7 -> 6 -> null
The time complexity of the above code is O(N) and space complexity is O(N) for the Set storage.
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Two Loops (Naive) | O(N²) | O(1) | Memory-constrained environments |
| Hashing (Set) | O(N) | O(N) | Better performance with larger lists |
Conclusion
We have implemented two approaches to remove duplicate elements from an unsorted linked list. The hashing approach with Set is more efficient with O(N) time complexity, while the naive approach uses constant space but has quadratic time complexity.
