Count duplicates in a given linked list

Given a linked list. The task is to count the number of duplicate nodes in the linked list.

Examples:

Input: 5 -> 7 -> 5 -> 1 -> 7 -> NULL
Output: 2

Input: 5 -> 7 -> 8 -> 7 -> 1 -> NULL
Output: 1

Simple Approach: 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.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
#include <unordered_set>
using namespace std;
  
// Representation of node
struct Node {
    int data;
    Node* next;
};
  
  
// Function to insert a node at the beginning
void insert(Node** head, int item)
{
    Node* temp = new Node();
    temp->data = item;
    temp->next = *head;
    *head = temp;
}
  
// Function to count the number of
// duplicate nodes in the linked list
int countNode(Node* head)
{
    int count = 0;
  
    while (head->next != NULL) {
  
        // Starting from the next node
        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;
}
  
// Driver code
int main()
{
    Node* head = NULL;
    insert(&head, 5);
    insert(&head, 7);
    insert(&head, 5);
    insert(&head, 1);
    insert(&head, 7);
  
    cout << countNode(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
      
// Representation of node 
static class Node 
    int data; 
    Node next; 
}; 
  
  
// Function to insert a node at the beginning 
static Node insert(Node head, int item) 
    Node temp = new Node(); 
    temp.data = item; 
    temp.next = head; 
    head = temp;
    return head;
  
// Function to count the number of 
// duplicate nodes in the linked list 
static int countNode(Node head) 
    int count = 0
  
    while (head.next != null
    
  
        // Starting from the next node 
        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; 
  
// Driver code 
public static void main(String args[])
    Node head = null
    head = insert(head, 5); 
    head = insert(head, 7); 
    head = insert(head, 5); 
    head = insert(head, 1); 
    head = insert(head, 7); 
  
    System.out.println( countNode(head)); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG
{
      
// Representation of node 
public class Node 
    public int data; 
    public Node next; 
}; 
  
  
// Function to insert a node at the beginning 
static Node insert(Node head, int item) 
    Node temp = new Node(); 
    temp.data = item; 
    temp.next = head; 
    head = temp;
    return head;
  
// Function to count the number of 
// duplicate nodes in the linked list 
static int countNode(Node head) 
    int count = 0; 
  
    while (head.next != null
    
  
        // Starting from the next node 
        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; 
  
// Driver code 
public static void Main(String []args)
    Node head = null
    head = insert(head, 5); 
    head = insert(head, 7); 
    head = insert(head, 5); 
    head = insert(head, 1); 
    head = insert(head, 7); 
  
    Console.WriteLine( countNode(head)); 
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

2

Time Complexity : O(n*n)

Efficient Approach: The idea is to use hashing

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
#include <unordered_set>
using namespace std;
  
// Representation of node
struct Node {
    int data;
    Node* next;
};
  
// Function to insert a node at the beginning
void insert(Node** head, int item)
{
    Node* temp = new Node();
    temp->data = item;
    temp->next = *head;
    *head = temp;
}
  
// Function to count the number of
// duplicate nodes in the linked list
int countNode(Node* head)
{
    if (head == NULL)
       return 0;;
  
    // Create a hash table insert head
    unordered_set<int> s;
    s.insert(head->data);
  
    // Traverse through remaining nodes    
    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 the count of duplicate nodes
    return count;
}
  
// Driver code
int main()
{
    Node* head = NULL;
    insert(&head, 5);
    insert(&head, 7);
    insert(&head, 5);
    insert(&head, 1);
    insert(&head, 7);
  
    cout << countNode(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.HashSet;
  
class GFG 
{
  
// Representation of node
static class Node
{
    int data;
    Node next;
};
static Node head;
  
// Function to insert a node at the beginning
static void insert(Node ref_head, int item)
{
    Node temp = new Node();
    temp.data = item;
    temp.next = ref_head;
    head = temp;
      
}
  
// Function to count the number of
// duplicate nodes in the linked list
static int countNode(Node head)
{
    if (head == null)
    return 0;;
  
    // Create a hash table insert head
    HashSet<Integer>s = new HashSet<>();
    s.add(head.data);
  
    // Traverse through remaining nodes 
    int count = 0;
    for (Node curr=head.next; curr != null; curr=curr.next) 
    {
        if (s.contains(curr.data))
            count++; 
  
        s.add(curr.data);
    }
  
    // Return the count of duplicate nodes
    return count;
}
  
// Driver code
public static void main(String[] args) 
{
  
    insert(head, 5);
    insert(head, 7);
    insert(head, 5);
    insert(head, 1);
    insert(head, 7);
  
    System.out.println(countNode(head));
}
}
  
// This code is contributed by Princi Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
      
class GFG 
{
  
// Representation of node
public class Node
{
    public int data;
    public Node next;
};
static Node head;
  
// Function to insert a node at the beginning
static void insert(Node ref_head, int item)
{
    Node temp = new Node();
    temp.data = item;
    temp.next = ref_head;
    head = temp;
      
}
  
// Function to count the number of
// duplicate nodes in the linked list
static int countNode(Node head)
{
    if (head == null)
    return 0;;
  
    // Create a hash table insert head
    HashSet<int>s = new HashSet<int>();
    s.Add(head.data);
  
    // Traverse through remaining nodes 
    int count = 0;
    for (Node curr=head.next; curr != null; curr=curr.next) 
    {
        if (s.Contains(curr.data))
            count++; 
  
        s.Add(curr.data);
    }
  
    // Return the count of duplicate nodes
    return count;
}
  
// Driver code
public static void Main(String[] args) 
{
  
    insert(head, 5);
    insert(head, 7);
    insert(head, 5);
    insert(head, 1);
    insert(head, 7);
  
    Console.WriteLine(countNode(head));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

2

Time Complexity : O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.