Related Articles

# Count duplicates in a given circular linked list

• Last Updated : 17 Sep, 2021

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
Explaination: The given list has 2 indices having integers which has already occured 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 ``using` `namespace` `std;`` ` `// Class to store Node of the list``class` `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 list``static` `int` `checkDuplicate(Node* head)``{``    ``if` `(head == NULL)``        ``return` `0;` `    ``// Stores the count of duplicate``    ``// integers``    ``int` `cnt = 0;` `    ``// Stores the integers occured``    ``set<``int``> s;``    ``s.insert(head->data);``    ``Node *curr = head->next;` `    ``// Loop to traverse the given list``    ``while` `(curr != head) {` `        ``// If integer already occured``        ``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 Code``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;``}`` ` `// This code is contributed by Dharanendra L V.`

## Java

 `// Java program for the above approach` `import` `java.util.HashSet;` `class` `GFG {` `    ``// Class to store Node of the list``    ``static` `class` `Node {``        ``int` `data;``        ``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 occured``        ``HashSet s = ``new` `HashSet<>();``        ``s.add(head.data);``        ``Node curr = head.next;` `        ``// Loop to traverse the given list``        ``while` `(curr != head) {` `            ``// If integer already occured``            ``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(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));``    ``}``}`
Output:
`2`

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up