Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Javascript Program For Removing Duplicates From An Unsorted Linked List

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. 
For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.

METHOD 1 (Using two loops): 
This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and the inner loop compares the picked element with the rest of the elements. 
Thanks to Gaurav Saxena for his help in writing this code.

Javascript




<script>
// 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
            {
                ptr2 = ptr2.next;
            }
        }
        ptr1 = ptr1.next;
    }
}
 
function printList(node)
{
    while (node != null)
    {
        document.write(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);
 
document.write(
         "Linked List before removing duplicates : <br/> ");
printList(head);
 
remove_duplicates();
document.write("<br/>");
document.write(
         "Linked List after removing duplicates : <br/> ");
printList(head);
// This code is contributed by aashish1995
</script>

Output: 

Linked list before removing duplicates:
10 12 11 11 12 11 10 
Linked list after removing duplicates:
10 12 11

Time Complexity: O(n^2)

Space Complexity: O(1), since it does not use any additional data structure to remove duplicates.

METHOD 2 (Use Sorting): 
In general, Merge Sort is the best-suited sorting algorithm for sorting linked lists efficiently. 
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn) 
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n) 
Please note that this method doesn’t preserve the original order of elements.
Time Complexity: O(nLogn)

METHOD 3 (Use Hashing): 
We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.

Javascript




<script>
// 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)
    {
        document.write(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);
 
document.write(
         "Linked list before removing duplicates :<br/>");
printList(start);
removeDuplicate(start);
 
document.write(
         "<br/>Linked list after removing duplicates :<br/>");
printList(start);
// This code is contributed by todaysgaurav
</script>

Output: 

Linked list before removing duplicates:
10 12 11 11 12 11 10 
Linked list after removing duplicates:
10 12 11

Thanks to bearwang for suggesting this method.
Time Complexity: O(n) on average (assuming that hash table access time is O(1) on average).

Space complexity: O(n), where n is the number of elements in the linked list.

Please refer complete article on Remove duplicates from an unsorted linked list for more details!


My Personal Notes arrow_drop_up
Last Updated : 03 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials