Count duplicates in a given circular linked list
Given a circular linked list, the task is to check whether the given list has duplicates or not.
Example:
Input: list = {5, 7, 5, 1, 4, 4}
Output: 2
Explanation: The given list has 2 indices having integers which has already occurred in the list during traversal.
Input: list = {1, 1, 1, 1, 1}
Output: 4
Approach: The given problem has a very similar solution to finding the count of duplicates in a linked list. The idea is to use hashing. Traverse the given circular linked list using the algorithm discussed here. Create a hashmap to store the integers that occurred in the list and for each integer, check if the integer has already occurred. Maintain the count of already occurred integers in a variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int data) {
this ->data = data;
this ->next = NULL;
}
};
static int checkDuplicate(Node* head)
{
if (head == NULL)
return 0;
int cnt = 0;
set< int > s;
s.insert(head->data);
Node *curr = head->next;
while (curr != head) {
if (s.find(curr->data) != s.end())
cnt++;
s.insert(curr->data);
curr = curr->next;
}
return cnt;
}
int main()
{
Node *head = new Node(5);
head->next = new Node(7);
head->next->next = new Node(5);
head->next->next->next = new Node(1);
head->next->next->next->next = new Node(4);
head->next->next->next->next->next = new Node(4);
head->next->next->next->next->next->next = head;
cout << checkDuplicate(head) << endl;
return 0;
}
|
Java
import java.util.HashSet;
public class GFG {
static class Node {
public int data;
public Node next;
public Node( int data) {
this .data = data;
}
}
static Node head;
static int checkDuplicate(Node head) {
if (head == null )
return 0 ;
int cnt = 0 ;
HashSet<Integer> s = new HashSet<Integer>();
s.add(head.data);
Node curr = head.next;
while (curr != head) {
if (s.contains(curr.data))
cnt++;
s.add(curr.data);
curr = curr.next;
}
return cnt;
}
public static void main(String[] args) {
head = new Node( 5 );
head.next = new Node( 7 );
head.next.next = new Node( 5 );
head.next.next.next = new Node( 1 );
head.next.next.next.next = new Node( 4 );
head.next.next.next.next.next = new Node( 4 );
head.next.next.next.next.next.next = head;
System.out.println(checkDuplicate(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = None ;
head = None ;
def checkDuplicate(head):
if (head = = None ):
return 0 ;
cnt = 0 ;
s = set ();
s.add(head.data);
curr = head. next ;
while (curr ! = head):
if ((curr.data) in s):
cnt + = 1 ;
s.add(curr.data);
curr = curr. next ;
return cnt;
if __name__ = = '__main__' :
head = Node( 5 );
head. next = Node( 7 );
head. next . next = Node( 5 );
head. next . next . next = Node( 1 );
head. next . next . next . next = Node( 4 );
head. next . next . next . next . next = Node( 4 );
head. next . next . next . next . next . next = head;
print (checkDuplicate(head));
|
C#
using System;
using System.Collections.Generic;
class GFG {
class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
}
};
static Node head;
static int checkDuplicate(Node head)
{
if (head == null )
return 0;
int cnt = 0;
HashSet< int > s = new HashSet< int >();
s.Add(head.data);
Node curr = head.next;
while (curr != head) {
if (s.Contains(curr.data))
cnt++;
s.Add(curr.data);
curr = curr.next;
}
return cnt;
}
public static void Main()
{
head = new Node(5);
head.next = new Node(7);
head.next.next = new Node(5);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(4);
head.next.next.next.next.next = new Node(4);
head.next.next.next.next.next.next = head;
Console.Write(checkDuplicate(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function checkDuplicate(head) {
if (head == null )
return 0;
var cnt = 0;
var s = new Set();
s.add(head.data);
var curr = head.next;
while (curr != head) {
if (s.has(curr.data))
cnt++;
s.add(curr.data);
curr = curr.next;
}
return cnt;
}
head = new Node(5);
head.next = new Node(7);
head.next.next = new Node(5);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(4);
head.next.next.next.next.next = new Node(4);
head.next.next.next.next.next.next = head;
document.write(checkDuplicate(head));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...