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
// Javascript program to remove duplicates from unsorted
// linked list
var head;
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
/*
* Function to remove duplicates from an unsorted linked list
*/
function remove_duplicates() {
var ptr1 = null, ptr2 = null, dup = null;
ptr1 = head;
/* Pick elements one by one */
while (ptr1 != null && ptr1.next != null) {
ptr2 = ptr1;
/*
* Compare the picked element with rest of the elements
*/
while (ptr2.next != null) {
/* If duplicate then delete it */
if (ptr1.data == ptr2.next.data) {
/* sequence of steps is important here */
dup = ptr2.next;
ptr2.next = ptr2.next.next;
} else /* This is tricky */ {
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);
// This code contributed by aashish1995
OutputLinked 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
// structure of a node in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// function to insert a node in the linked list
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 to sort the linked list
function sortList(head) {
// pointer to traverse the linked list
let current = head;
let index = null;
// loop to traverse the linked list
while (current !== null) {
// loop to compare current node with all other nodes
index = current.next;
while (index !== null) {
// checking for duplicate values
if (current.data === index.data) {
// deleting the duplicate node
current.next = index.next;
index = current.next;
}
else {
index = index.next;
}
}
current = current.next;
}
}
// function to display the linked list
function printList(node) {
let str = "";
while (node !== null) {
str += node.data + " ";
node = node.next;
}
console.log(str);
}
// main function
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);
OutputLinked 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
// JavaScript program to remove duplicates
// from unsorted linkedlist
class node {
constructor(val) {
this.val = val;
this.next = null;
}
}
/*
Function to remove duplicates
from a unsorted linked list
*/
function removeDuplicate( head) {
// Hash to store seen values
var hs = new Set();
/* Pick elements one by one */
var current = head;
var prev = null;
while (current != null) {
var curval = current.val;
// If current value is seen before
if (hs.has(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a
given linked list */
function printList( head) {
while (head != null) {
console.log(head.val + " ");
head = head.next;
}
}
/*
* The constructed linked list is:
10->12->11->11->12->11->10
*/
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);
// This code is contributed by todaysgaurav
OutputLinked 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.
Similar Reads
JavaScript Linked List Programs
JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program
5 min read
Implementation of LinkedList in Javascript
In this article, we will be implementing the LinkedList data structure in Javascript.A linked list is a linear data structure where elements are stored in nodes, each containing a value and a reference (or pointer) to the next node. It allows for efficient insertion and deletion operations.Each node
5 min read
Javascript Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise.bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functio
3 min read
Javascript Program For Inserting A Node In A Linked List
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node
7 min read
Javascript Program For Inserting Node In The Middle Of The Linked List
Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node.Examples: Input : list: 1->2->4->5 x = 3Output : 1->2->3-
4 min read
Javascript Program For Writing A Function To Delete A Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list.Implementation: JavaScript// Javascript program to delete // a li
1 min read
Javascript Program For Deleting A Linked List Node At A Given Position
Given a singly linked list and a position, delete a linked list node at the given position.Example: Input: position = 1, Linked List = 8->2->3->1->7Output: Linked List = 8->3->1->7Input: position = 0, Linked List = 8->2->3->1->7Output: Linked List = 2->3->1-
3 min read
Javascript Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list.For example, the function should return 5 for linked list 1->3->1->2->1.Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head.3) Do following while current is not NULL a) curre
3 min read
Javascript Program For Rotating A Linked List
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal
5 min read
Javascript Program For Making Middle Node Head In A Linked List
Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples:Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves on
3 min read