
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count minimum frequency elements in a linked list in C++
Given the task is to count the minimum frequency elements in a given linked list which is having duplicate elements.
A linked list is a data structure in which the data is stored in the serial order, like a list each element is linked to the next element.
The frequency of an element in a linked list refers to the number of times an element is occurring in the linked list. According to the problem we have to count the minimum frequency in a linked list.
Let us suppose we have a linked list, 1, 1, 3, 1, 3, 4, 6; where the minimum frequency is one so we have to count the elements, who are having a minimum frequency. There are only two elements 4 and 6 with the least frequency so the count is 2.
Input −
linked list 1->1->2->2->2->3->3
Output −
count is 2
Explanation −
The minimum frequency in the above example is 2 and there are two elements with the minimum frequency i.e. 1 and 3, so the count is 2.
Input −
linked list = 1->2->3->2->4->2->5
Output −
count is 4
Explanation −
The minimum frequency in the above example is 1 and there are 4 elements with the minimum frequency i.e. 1, 3, 4, and 5, so the count is 4.
Approach used in the below program as follows
Define a linked list and push the elements in the linked list.
In the minimum function to find the count of the elements with minimum frequency, declare a map “mymap” to store the frequencies of numbers.
Traverse the list and store the frequency (occurrence) of the elements in mymap.
After we found the frequencies and stored the frequencies in mymap, then find the minimum frequency.
Count the frequency number of times that occurred in the mymap.
Return the count.
Example
#include <iostream> #include <unordered_map> #include <climits> using namespace std; struct Node { int key; struct Node* next; }; // to push the values in the stack 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 minimum(struct Node* head){ // Store frequencies of all nodes. unordered_map<int, int> mymap; struct Node* current = head; while (current != NULL){ int value = current->key; mymap[value]++; current = current->next; } // Find min frequency current = head; int min = INT_MAX, count = 0; for (auto it = mymap.begin(); it != mymap.end(); it++){ if (it->second <= min){ min = it->second; } } // Find count of min frequency elements for (auto it = mymap.begin(); it != mymap.end(); it++){ if (it->second == min){ count += (it->second); } } return count; } int main(){ /* Starting with an empty list */ struct Node* head = NULL; int x = 21; push(&head, 30); push(&head, 50); push(&head, 61); push(&head, 40); push(&head, 30); cout <<"count is: "<<minimum(head) << endl; return 0; }
Output
If we run the above code we will get the following output −
count is: 3
- Related Articles
- Find minimum and maximum elements in singly Circular Linked List in C++
- Count nodes in Circular linked list in C++
- Count rotations in sorted and rotated linked list in C++
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Sum of smaller elements of nodes in a linked list in C++
- List frequency of elements in Python
- Python – Restrict Elements Frequency in List
- C++ Pairwise Swap Elements of a Given Linked List
- Explain insertion of elements in linked list using C language
- Find smallest and largest elements in singly linked list in C++
- Check if a linked list is Circular Linked List in C++
- Count Frequency of Highest Frequent Elements in Python
- Clear a Linked List in C#
- Flattening a Linked list in C++
- Python – Fractional Frequency of elements in List
