# Count of Nodes in a LinkedList whose value is equal to their frequency

Given a Singly linked list, the task is to count the number of nodes whose data value is equal to its frequency.

**Examples: **

Input:Linked list = 2 -> 3 -> 3 -> 3 -> 4 -> 2

Output:2

Frequency of element 2 is 2

Frequency of element 3 is 3

Frequency of element 4 is 1

So, 2 and 3 are elements which have same frequency as it’s value

Input:Linked list = 1 -> 2 -> 3 -> 4 -> 5 -> 6

Output:1

## Recommended: Please try your approach on __{IDE}__ first, before moving on to the solution.

__{IDE}__**Approach:**

Approach to solve this problem is as following

- Iterate over the linked list and store the frequency of every element of the array using a map
- Iterate over the map and count the number of elements whose frequency is equal to their value

Below is the implementation of the above approach:

`/* Link list node */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `class` `Node { ` `public` `: ` ` ` `int` `data; ` ` ` `Node* next; ` `}; ` ` ` `// Function to add a node at the ` `// beginning of List ` `void` `push(Node** head_ref, ` `int` `data) ` `{ ` ` ` `/* allocate node */` ` ` `Node* new_node =` `new` `Node(); ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = data; ` ` ` ` ` `// link the old list off the new ` ` ` `// node ` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `// move the head to point to the ` ` ` `// new node ` ` ` `(*head_ref) = new_node; ` `} ` ` ` ` ` `// Counts the no. of occurences of a ` `// node in a linked list ` `int` `countValuesWithSameFreq(Node* start) ` `{ ` ` ` `map<` `int` `, ` `int` `> mpp; ` ` ` `Node* current = start; ` ` ` `int` `count = 0; ` ` ` `while` `(current != NULL) { ` ` ` `mpp[current->data] += 1; ` ` ` `current = current->next; ` ` ` `} ` ` ` `int` `ans = 0; ` ` ` `for` `(` `auto` `x : mpp) { ` ` ` `int` `value = x.first; ` ` ` `int` `freq = x.second; ` ` ` ` ` `// Check if value equls to frequency ` ` ` `// and increment the count ` ` ` `if` `(value == freq) { ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// main program ` `int` `main() ` `{ ` ` ` `Node* head = NULL; ` ` ` `push(&head, 3); ` ` ` `push(&head, 4); ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 2); ` ` ` `push(&head, 3); ` ` ` ` ` `cout << countValuesWithSameFreq(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Complexity Analysis:**

**Time Complexity: **For a given linked list of size **n**, we are iterating over it once. So the time complexity of this solution is **O(n)**

**Space Complexity: **For a given linked list of size **n**, we are using an extra map which can have maximum of n key-values, so space complexity of this solution is **O(n)**

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Sum of all odd frequency nodes of the Linked List
- Check if frequency of each character is equal to its position in English Alphabet
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count minimum frequency elements in a linked list
- Count nodes in Circular linked list
- Count of Nodes which has Prime Digit sum weight in a Tree
- Count of Prime Nodes of a Singly Linked List
- Count nodes in the given tree whose weight is a fibonacci number
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Find count of common nodes in two Doubly Linked Lists
- Count pairs from two linked lists whose sum is equal to a given value
- Check whether count of odd and even factors of a number are equal
- Count pairs in a binary tree whose sum is equal to a given value x
- Generate an array of given size with equal count and sum of odd and even numbers
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count pairs from two linked lists whose product is equal to a given value
- Check if count of even divisors of N is equal to count of odd divisors
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x
- Count triplets in a sorted doubly linked list whose product is equal to a given value x

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.