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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â Â // Class to store Node of the listclass Node {public:Â Â Â Â int data;Â Â Â Â Node* next;Â Â Â Â Â Â Â Â Â Node(int data) {Â Â Â Â Â Â Â Â Â this->data = data;Â Â Â Â Â Â Â Â Â Â this->next = NULL;Â Â Â Â }};Â Â // Function to find the count of the// duplicate integers in the liststatic int checkDuplicate(Node* head){Â Â Â Â if (head == NULL)Â Â Â Â Â Â Â Â return 0;Â
    // Stores the count of duplicate    // integers    int cnt = 0;Â
    // Stores the integers occurred    set<int> s;    s.insert(head->data);    Node *curr = head->next;Â
    // Loop to traverse the given list    while (curr != head) {Â
        // If integer already occurred        if (s.find(curr->data) != s.end())            cnt++;Â
        // Add current integer into        // the hashmap        s.insert(curr->data);        curr = curr->next;    }Â
    // Return answer    return cnt;}Â
// Driver Codeint 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;}  // This code is contributed by Dharanendra L V. |
Java
// Java program for the above approachÂ
import java.util.HashSet;Â
public class GFG {Â
    // Class to store Node of the list    static class Node {        public int data;        public Node next;Â
        public Node(int data) {            this.data = data;        }    }Â
    // Stores head pointer of the list    static Node head;Â
    // Function to find the count of the    // duplicate integers in the list    static int checkDuplicate(Node head) {        if (head == null)            return 0;Â
        // Stores the count of duplicate        // integers        int cnt = 0;Â
        // Stores the integers occurred        HashSet<Integer> s = new HashSet<Integer>();        s.add(head.data);        Node curr = head.next;Â
        // Loop to traverse the given list        while (curr != head) {Â
            // If integer already occurred            if (s.contains(curr.data))                cnt++;Â
            // Add current integer into            // the hashset            s.add(curr.data);            curr = curr.next;        }Â
        // Return answer        return cnt;    }Â
    // Driver Code    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));    }}Â
// this code is contributed by bhardwajji |
Python3
# Python program for the above approachÂ
# Class to store Node of the listclass Node: Â Â Â Â def __init__(self, data):Â Â Â Â Â Â Â Â self.data = data; Â Â Â Â Â Â Â Â self.next = None;Â Â Â Â Â # Stores head pointer of the listhead = None;Â
# Function to find the count of the# duplicate integers in the listdef checkDuplicate(head):Â Â Â Â if (head == None):Â Â Â Â Â Â Â Â return 0;Â
    # Stores the count of duplicate    # integers    cnt = 0;Â
    # Stores the integers occurred    s = set();    s.add(head.data);    curr = head.next;Â
    # Loop to traverse the given list    while (curr != head):Â
        # If integer already occurredA        if ((curr.data) in s):            cnt+=1;Â
        # Add current integer into        # the hashmap        s.add(curr.data);        curr = curr.next;         # Return answer    return cnt;Â
# Driver Codeif __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));Â
# This code is contributed by umadevi9616 |
C#
// C# program for the above approachusing System;using System.Collections.Generic;Â
class GFG {Â
    // Class to store Node of the list     class Node {        public int data;        public Node next;Â
        public Node(int data)        {            this.data = data;        }    };Â
    // Stores head pointer of the list    static Node head;Â
    // Function to find the count of the    // duplicate integers in the list    static int checkDuplicate(Node head)    {        if (head == null)            return 0;Â
        // Stores the count of duplicate        // integers        int cnt = 0;Â
        // Stores the integers occurred        HashSet<int> s = new HashSet<int>();        s.Add(head.data);        Node curr = head.next;Â
        // Loop to traverse the given list        while (curr != head) {Â
            // If integer already occurred            if (s.Contains(curr.data))                cnt++;Â
            // Add current integer into            // the hashmap            s.Add(curr.data);            curr = curr.next;        }Â
        // Return answer        return cnt;    }Â
    // Driver Code    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));    }}Â
// This code is contributed by ipg2016107. |
Javascript
<script>// javascript program for the above approachÂ
    // Class to store Node of the listclass Node {    constructor(val) {        this.data = val;        this.next = null;    }}Â
    // Stores head pointer of the list    var head;Â
    // Function to find the count of the    // duplicate integers in the list    function checkDuplicate(head) {        if (head == null)            return 0;Â
        // Stores the count of duplicate        // integers        var cnt = 0;Â
        // Stores the integers occurred        var s = new Set();        s.add(head.data);var curr = head.next;Â
        // Loop to traverse the given list        while (curr != head) {Â
            // If integer already occurred            if (s.has(curr.data))                cnt++;Â
            // Add current integer into            // the hashmap            s.add(curr.data);            curr = curr.next;        }Â
        // Return answer        return cnt;    }Â
    // Driver Code        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));Â
// This code is contributed by gauravrajput1 </script> |
2
Â
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
