Count of Nodes in a LinkedList whose value is equal to their frequency
Last Updated :
09 Mar, 2023
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
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:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void push(Node** head_ref, int data)
{
Node* new_node = new Node();
new_node->data = data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
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;
if (value == freq) {
ans++;
}
}
return ans;
}
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;
}
|
Java
import java.util.*;
class GFG{
public static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int countValuesWithSameFreq(Node start)
{
HashMap<Integer, Integer> mpp = new HashMap<>();
Node current = start;
while (current != null )
{
if (mpp.containsKey(current.data))
{
mpp.put(current.data,
mpp.get(current.data) + 1 );
}
else
{
mpp.put(current.data, 1 );
}
current = current.next;
}
int ans = 0 ;
for (Map.Entry<Integer,
Integer> x : mpp.entrySet())
{
int value = x.getKey();
int freq = x.getValue();
if (value == freq)
{
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 3 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 2 );
head = push(head, 3 );
System.out.print(countValuesWithSameFreq(head));
}
}
|
Python3
class Node:
def __init( self , next ):
self .data = 0
self . next = None
def push(head_ref, data):
new_node = Node()
new_node.data = data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def countValuesWithSameFreq(start):
mpp = dict ()
current = start
count = 0
while (current ! = None ):
if current.data not in mpp:
mpp[current.data] = 0
mpp[current.data] + = 1
current = current. next
ans = 0
for x in mpp.keys():
value = x
freq = mpp[x]
if (value = = freq):
ans + = 1
return ans
if __name__ = = '__main__' :
head = None
head = push(head, 3 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 2 )
head = push(head, 3 )
print (countValuesWithSameFreq(head))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int countValuesWithSameFreq(Node start)
{
Dictionary< int ,
int > mpp = new Dictionary< int ,
int >();
Node current = start;
while (current != null )
{
if (mpp.ContainsKey(current.data))
{
mpp[current.data] = mpp[current.data] + 1;
}
else
{
mpp.Add(current.data, 1);
}
current = current.next;
}
int ans = 0;
foreach (KeyValuePair< int , int > x in mpp)
{
int value = x.Key;
int freq = x.Value;
if (value == freq)
{
ans++;
}
}
return ans;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 3);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
Console.Write(countValuesWithSameFreq(head));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function countValuesWithSameFreq(start)
{
var mpp = new Map();
var current = start;
while (current != null )
{
if (mpp.has(current.data))
{
mpp.set(current.data , mpp.get(current.data)+1);
}
else
{
mpp.set(current.data, 1);
}
current = current.next;
}
var ans = 0;
mpp.forEach((value, key) => {
var value = value;
var freq = key;
if (value == freq)
{
ans++;
}
});
return ans;
}
var head = null ;
head = push(head, 3);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
document.write(countValuesWithSameFreq(head));
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...