Count minimum frequency elements in a linked list
Given a linked list containing duplicate elements. The task is to find the count of all minimum occurring elements in the given linked list. That is the count of all such elements whose frequency is minimum in the matrix.
Examples:
Input : 1-> 2-> 2-> 3 Output : 2 Explanation: 1 and 3 are elements occurs only one time. So, count is 2. Input : 10-> 20-> 20-> 10-> 30 Output : 1
Approach:
- Traverse the linked list and use a hash table to store the frequency of elements of the linked list such that the key of map is the linked list element and value is its frequency in the linked list.
- Then traverse the hash table to find the minimum frequency.
- Finally, traverse the hash table to find the frequency of elements and check if it matches with the minimum frequency obtained in previous step, if yes, then add this frequency to count.
Below is the implementation of the above approach:
C++
// C++ program to find count of minimum // frequqncy elements in Linked list #include <bits/stdc++.h> using namespace std; /* Link list node */struct Node { int key; struct Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */void push(struct Node** head_ref, int new_key) { struct Node* new_node = new Node; new_node->key = new_key; new_node->next = (*head_ref); (*head_ref) = new_node; } // Function to count minimum frequency elements // in the linked list int countMinimum(struct Node* head) { // Store frequencies of all nodes. unordered_map<int, int> mp; struct Node* current = head; while (current != NULL) { int data = current->key; mp[data]++; current = current->next; } // Find min frequency current = head; int min_frequency = INT_MAX, countMin = 0; for (auto it = mp.begin(); it != mp.end(); it++) { if (it->second <= min_frequency) { min_frequency = it->second; } } // Find count of min frequency elements for (auto it = mp.begin(); it != mp.end(); it++) { if (it->second == min_frequency) { countMin += (it->second); } } return countMin; } /* Driver program to test count function*/int main() { /* Start with the empty list */ struct Node* head = NULL; int x = 21; /* Use push() to construct below list 10->10->11->30->10 */ push(&head;, 10); push(&head;, 30); push(&head;, 11); push(&head;, 10); push(&head;, 10); cout << countMinimum(head) << endl; return 0; } |
chevron_right
filter_none
Python3
# Python3 program to find count of minimum # frequqncy elements in Linked list import sys import math # Link list node class Node: def __init__(self,data): self.data = data self.next = None # Given a reference (pointer to pointer) to the head # of a list and an int, push a new node on the front # of the list. def push(head,data): if not head: return Node(data) temp = Node(data) temp.next = head head = temp return head # Function to count minimum frequency elements # in the linked list def countMinimun(head): # Store frequencies of all nodes. freq = {} temp = head while(temp): d = temp.data if d in freq: freq[d] = freq.get(d) + 1 else: freq[d] = 1 temp = temp.next # Find min frequency minimum_freq = sys.maxsize for i in freq: minimum_freq = min(minimum_freq, freq.get(i)) # Find count of min frequency elements countMin = 0 for i in freq: if freq.get(i) == minimum_freq: countMin += 1 return countMin # Driver program to test count function # if __name__=='__main__': # Start with the empty list head = None # Use push() to construct below list #10->10->11->30->10 head = push(head,10) head = push(head,30) head = push(head,11) head = push(head,10) head = push(head,10) print(countMinimun(head)) # This code is Contribute by Vikash Kumar 37 |
chevron_right
filter_none
Output:
2
Time Complexity : O(n)
Recommended Posts:
- Find minimum and maximum elements in singly Circular Linked List
- Sum of all odd frequency nodes of the Linked List
- Sum of all minimum frequency elements in Matrix
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Count duplicates in a given linked list
- Reverse first K elements of given linked list
- Reverse even elements in a Linked List
- Count nodes in Circular linked list
- Sum of smaller elements of nodes in a linked list
- Find unique elements in linked list
- Pairwise swap elements of a given linked list
- Count of Prime Nodes of a Singly Linked List
- Count rotations in sorted and rotated linked list
- Replace each node with its Surpasser Count in Linked List
- Minimum and Maximum Prime Numbers of a Singly Linked List
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.
Improved By : Vikash Kumar 37



