Count duplicates in a Linked List

This article will help you to understand to find duplicate elements in linked list. Having knowledge about the linked lists will help you for clearing IT interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview. Now, Let’s move to our topic to find duplicate in linked list.

Problem Statement

In this question, we are given a singly linked list. We have to count the number of duplicate nodes in the linked list

Problem Statement Understanding To Find Duplicate Elements In Linked List

Let’s first understand the problem statement with help of examples.

Suppose the given list is:

We have to count the number of duplicate nodes in the list.

From the above given linked list, we can see that:

  • Count of each 1 and 8 is 2 in the linked list.
  • While the count of 7 is 1 in the linked list.

So, we can say that duplicates of 1 and 8 exist in the linked list, 1 duplicate each of 1 and 8 exists in the linked list. So, we will return the count of duplicate node in the linked list as (1+1) = 2.

If the given linked list is:

For the above-linked list, we can see that:

  • Count of each 1, 2, 3 is 2 in the linked list.
  • The count of 5 is 3.
  • While the count of 4 is 1 in the linked list.

So, we can say that duplicates of 1, 2, 3, and 5 exist in the linked list, 1 duplicate each of 1, 2, and 3 exist and 2 duplicates of 5 exist. So, we will return the count of duplicate node in the linked list as (1+1+1+2) = 5.

Now, I think it is clear from the examples what we have to do with the problem. So let’s move to approach.

Before jumping to approach, just try to think how can you approach this problem?

It’s okay if your solution is brute force, we will try to optimize it together.

We will first make use of simple nested list traversal to find the duplicates, although it will be a brute-force approach, it is necessary to build the foundation.

Let us have a glance at brute force approaches.

Approach and Algorithm (Brute Force) To Find Duplicate Elements In Linked List

The approach is going to be pretty simple.

  • We will create a variable count and initialize it to 0.
  • Now, we will traverse through the list, and for every node, we will traverse from its next node to the end of the list whenever we will find a match, we will increase the counter and will break out of the loop. Basically here what we are trying to find out is that is this particular node duplicate or not.

This method has a time complexity of O(n2) as it is using nested traversal.

Can we do better?
Yes. We can do better. We can do it in O(n) with the help of hashing.

Let us see the efficient approach.

Approach (Hashing) To Find Duplicate Elements In Linked List

In this approach, we will use hashing.

  • Firstly, we will create an unordered set, and we will insert the data of the head in the set.
  • We will also create a counter whose initial value will be 0.
  • Now, we will traverse through the list, starting from the next node, and for every node, we will check if that node is already present in the set or not. If it is present, we will increment the counter. We will also keep inserting the nodes, that we are traversing, in the set.

Algorithm To Find Duplicate Elements In Linked List

  • Base Case – If the head is NULL, return 0.
  • Create an unordered set, say s, and a variable count with an initial value of 0.
  • Insert the head → data in the set.
  • Traverse from the next node of the head till the end of the list.
  • In every iteration, check if the node’s data is already present in the set or not. If present, increment the count.
  • Insert the current node’s data in the set.
  • In the end, return the count variable.

Dry Run To Find Duplicate Elements In Linked List

Code Implementation

#include<stdio.h>
#include<stdlib.h>

struct Node { 
    int data; 
    struct Node* next; 
}; 


// Function to insert a node at the beginning 
void insert(struct Node** head, int item) 
{ 
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = item; 
    temp->next = *head; 
    *head = temp; 
} 

// Function to count the number of 
// duplicate nodes in the linked list 
int countNode(struct Node* head) 
{ 
    int count = 0; 

    while (head->next != NULL) { 

        // Starting from the next node 
        struct Node *ptr = head->next; 
        while (ptr != NULL) { 

            // If some duplicate node is found 
            if (head->data == ptr->data) { 
                count++; 
                break; 
            } 
            ptr = ptr->next; 
        } 

        head = head->next; 
    } 

    // Return the count of duplicate nodes 
    return count; 
} 

// Main function
int main() 
{ 
    struct Node* head = NULL; 
    insert(&head, 5); 
    insert(&head, 7); 
    insert(&head, 5); 
    insert(&head, 4); 
    insert(&head, 7); 

    int ans = countNode(head); 
    printf("%d", ans);
    return 0; 
} 
#include <iostream>
#include <unordered_set>
using namespace std;

struct Node {
    int data;
    Node* next;
};

void insert(Node** head, int item)
{
    Node* temp = new Node();
    temp->data = item;
    temp->next = *head;
    *head = temp;
}

int countNode(Node* head)
{
    if (head == NULL)
       return 0;;

    unordered_set<int> s;
    s.insert(head->data);

    int count = 0;
    for (Node *curr=head->next; curr != NULL; curr=curr->next) {
        if (s.find(curr->data) != s.end())
             count++;
 
        s.insert(curr->data);
    }

    return count;
}
 

int main()
{
    Node* head = NULL;
    insert(&head, 8);
    insert(&head, 8);
    insert(&head, 1);
    insert(&head, 7);
    insert(&head, 1);
 
    cout << countNode(head);
 
    return 0;
}

import java.util.HashSet;
 
public class PrepBytes
{
 

static class Node
{
    int data;
    Node next;
};
static Node head;

static void insert(Node ref_head, int item)
{
    Node temp = new Node();
    temp.data = item;
    temp.next = ref_head;
    head = temp;
     
}

static int countNode(Node head)
{
    if (head == null)
    return 0;;

    HashSet<integer>s = new HashSet<>();
    s.add(head.data);

    int count = 0;
    for (Node curr=head.next; curr != null; curr=curr.next)
    {
        if (s.contains(curr.data))
            count++;
 
        s.add(curr.data);
    }

    return count;
}

public static void main(String[] args)
{
 
    insert(head, 8);
    insert(head, 8);
    insert(head, 1);
    insert(head, 7);
    insert(head, 1);
 
    System.out.println(countNode(head));
}
}

class Node:
    def __init__(self, data = None, next = None):
        self.next = next
        self.data = data

head = None

def insert(ref_head, item):
    global head
    temp = Node()
    temp.data = item
    temp.next = ref_head
    head = temp
    
def countNode(head):

    if (head == None):
        return 0

    s = set()
    s.add(head.data)
    count = 0
    curr = head.next
    while ( curr != None ) :
        if (curr.data in s):
            count = count + 1

        s.add(curr.data)
        curr = curr.next
    return count

insert(head, 8)
insert(head, 8)
insert(head, 1)
insert(head, 7)
insert(head, 1)

print(countNode(head))

Output

2

Time Complexity To Find Duplicate Elements In Linked List: O(n), as list traversal is needed.

This article will help you to understand how to find duplicate in linked list. There is a lot of stuff to prepare for the interviews but data structures and algorithms are the most important topics as these are the must needed skills for any company. This is an important question when it comes to coding interviews. If you want to solve more questions on Linked List, which is curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

FAQ Related To Find Duplicate Elements In Linked List

  1. What is the time complexity for searching for an element on a Linked List?
  2. Since there is no way of accessing any node without visiting its previous node, we have to traverse the entire Linked List every time we need to search for an element. Therefore, the worst-case time complexity is O(N) where N is the number of elements in a Linked List.

  3. How do you find duplicates in a linked list?
  4. We traverse the whole linked list. For each node, we check in the remaining list whether the duplicate node exists or not. If it does then we increment the count.

  5. Is a linked list dynamic?
  6. Linked List is a dynamic structure, which means the list can grow or shrink depending on the data making it more powerful and flexible than Arrays. Unlike Arrays, Linked List is not stored in a contiguous memory location. Each element in the list is spread across the memory and are linked by the pointers in the Node.

Leave a Reply

Your email address will not be published. Required fields are marked *