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++
#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)
{
int count = 0;
while (head->next != NULL) {
Node *ptr = head->next;
while (ptr != NULL) {
if (head->data == ptr->data) {
count++;
break ;
}
ptr = ptr->next;
}
head = head->next;
}
return count;
}
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;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node insert(Node head, int item)
{
Node temp = new Node();
temp.data = item;
temp.next = head;
head = temp;
return head;
}
static int countNode(Node head)
{
int count = 0 ;
while (head.next != null )
{
Node ptr = head.next;
while (ptr != null )
{
if (head.data == ptr.data)
{
count++;
break ;
}
ptr = ptr.next;
}
head = head.next;
}
return count;
}
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));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head, item):
temp = Node(item);
temp.data = item;
temp. next = head;
head = temp;
return head;
def countNode(head):
count = 0
while (head. next ! = None ):
ptr = head. next
while (ptr ! = None ):
if (head.data = = ptr.data):
count = count + 1
break
ptr = ptr. next
head = head. next
return count
if __name__ = = '__main__' :
head = None ;
head = push(head, 5 )
head = push(head, 7 )
head = push(head, 5 )
head = push(head, 1 )
head = push(head, 7 )
print (countNode(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node insert(Node head, int item)
{
Node temp = new Node();
temp.data = item;
temp.next = head;
head = temp;
return head;
}
static int countNode(Node head)
{
int count = 0;
while (head.next != null )
{
Node ptr = head.next;
while (ptr != null )
{
if (head.data == ptr.data)
{
count++;
break ;
}
ptr = ptr.next;
}
head = head.next;
}
return count;
}
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));
}
}
|
Javascript
<script>
class Node {
constructor() {
var data;
var next;
}
}
function insert( head, item)
{
var temp = new Node();
temp.data = item;
temp.next = head;
head = temp;
return head;
}
function countNode( head)
{
let count = 0;
while (head.next != null )
{
let ptr = head.next;
while (ptr != null )
{
if (head.data == ptr.data)
{
count++;
break ;
}
ptr = ptr.next;
}
head = head.next;
}
return count;
}
var head = null ;
head = insert(head, 5);
head = insert(head, 7);
head = insert(head, 5);
head = insert(head, 1);
head = insert(head, 7);
document.write( countNode(head));
</script>
|
Time Complexity: O(n*n)
Efficient Approach: The idea is to use hashing
C++
#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, 5);
insert(&head, 7);
insert(&head, 5);
insert(&head, 1);
insert(&head, 7);
cout << countNode(head);
return 0;
}
|
Java
import java.util.HashSet;
class GFG
{
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, 5 );
insert(head, 7 );
insert(head, 5 );
insert(head, 1 );
insert(head, 7 );
System.out.println(countNode(head));
}
}
|
Python
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, 5 )
insert(head, 7 )
insert(head, 5 )
insert(head, 1 )
insert(head, 7 )
print (countNode(head))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public 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< int >s = new HashSet< int >();
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, 5);
insert(head, 7);
insert(head, 5);
insert(head, 1);
insert(head, 7);
Console.WriteLine(countNode(head));
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function insert(head, item)
{
var temp = new Node();
temp.data = item;
temp.next = head;
head = temp;
return head;
}
function countNode(head)
{
if (head == null )
return 0;;
var s = new Set();
s.add(head.data);
var count = 0;
for ( var curr=head.next; curr != null ; curr=curr.next) {
if (s.has(curr.data))
count++;
s.add(curr.data);
}
return count;
}
var head = null ;
head = insert(head, 5);
head = insert(head, 7);
head = insert(head, 5);
head = insert(head, 1);
head = insert(head, 7);
document.write( countNode(head));
</script>
|
Time Complexity : O(n)
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...