# 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.

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 ` `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; ` `} `

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)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.