Javascript Program For Removing Duplicates From An Unsorted Linked List
Last Updated :
28 Jun, 2023
Given an unsorted Linked List, the task is to remove duplicates from the list.
Examples:
Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21
Output: 12 -> 11 -> 21 -> 41 -> 43
Explanation: Second occurrence of 12 and 21 are removed.
Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21
Output: 12 -> 11 -> 21 -> 41 -> 43
Naive Approach to Remove Duplicates from an Unsorted Linked List:
The most simple approach to solve this, is to check each node for duplicate in the Linked List one by one.
Below is the Implementation of the above approach:
Javascript
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function remove_duplicates() {
var ptr1 = null , ptr2 = null , dup = null ;
ptr1 = head;
while (ptr1 != null && ptr1.next != null ) {
ptr2 = ptr1;
while (ptr2.next != null ) {
if (ptr1.data == ptr2.next.data) {
dup = ptr2.next;
ptr2.next = ptr2.next.next;
} else {
ptr2 = ptr2.next;
}
}
ptr1 = ptr1.next;
}
}
function printList( node) {
while (node != null ) {
console.log(node.data + " " );
node = node.next;
}
}
head = new Node(10);
head.next = new Node(12);
head.next.next = new Node(11);
head.next.next.next = new Node(11);
head.next.next.next.next = new Node(12);
head.next.next.next.next.next = new Node(11);
head.next.next.next.next.next.next = new Node(10);
console.log( "Linked List before removing duplicates : <br/> " );
printList(head);
remove_duplicates();
console.log( "<br/>" );
console.log( "Linked List after removing duplicates : <br/> " );
printList(head);
|
Output
Linked List before removing duplicates : <br/>
10
12
11
11
12
11
10
<br/>
Linked List after removing duplicates : <br/>
10
12
11
Time Complexity: O(N2)
Auxiliary Space: O(1)
Remove duplicates from an Unsorted Linked List using Sorting:
Follow the below steps to Implement the idea:
Below is the implementation for above approach:
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function push(head_ref, new_data) {
const new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function sortList(head) {
let current = head;
let index = null ;
while (current !== null ) {
index = current.next;
while (index !== null ) {
if (current.data === index.data) {
current.next = index.next;
index = current.next;
}
else {
index = index.next;
}
}
current = current.next;
}
}
function printList(node) {
let str = "" ;
while (node !== null ) {
str += node.data + " " ;
node = node.next;
}
console.log(str);
}
let head = null ;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
console.log( "Linked List before removing duplicates : " );
printList(head);
sortList(head);
console.log( "Linked List after removing duplicates : " );
printList(head);
|
Output
Linked List before removing duplicates :
11 11 11 13 13 20
Linked List after removing duplicates :
11 13 20
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Remove duplicates from an Unsorted Linked List using Hashing:
The idea for this approach is based on the following observations:
- Traverse the link list from head to end.
- For every newly encountered element, check whether if it is in the hash table:
- if yes, remove it
- otherwise put it in the hash table.
- At the end, the Hash table will contain only the unique elements.
Below is the implementation of the above approach:
Javascript
class node {
constructor(val) {
this .val = val;
this .next = null ;
}
}
function removeDuplicate( head) {
var hs = new Set();
var current = head;
var prev = null ;
while (current != null ) {
var curval = current.val;
if (hs.has(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
function printList( head) {
while (head != null ) {
console.log(head.val + " " );
head = head.next;
}
}
start = new node(10);
start.next = new node(12);
start.next.next = new node(11);
start.next.next.next = new node(11);
start.next.next.next.next = new node(12);
start.next.next.next.next.next = new node(11);
start.next.next.next.next.next.next = new node(10);
console.log(
"Linked list before removing duplicates :<br/>"
);
printList(start);
removeDuplicate(start);
console.log(
"<br/>Linked list after removing duplicates :<br/>"
);
printList(start);
|
Output
Linked list before removing duplicates :<br/>
10
12
11
11
12
11
10
<br/>Linked list after removing duplicates :<br/>
10
12
11
Time Complexity: O(N), on average (assuming that hash table access time is O(1) on average).
Auxiliary Space: O(N), As extra space is used to store the elements in the stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...