- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- Find minimum and maximum elements in singly Circular Linked List in C++
- List frequency of elements in Python
- Python – Restrict Elements Frequency in List
- Count Frequency of Highest Frequent Elements in Python
- Count nodes in Circular linked list in C++
- Add elements to a linked list using Javascript
- Remove elements from a linked list using Javascript
- Python – Fractional Frequency of elements in List
- Remove elements from singly linked list in JavaScript
- Python – Count frequency of sublist in given list
- Minimum and Maximum Prime Numbers of a Singly Linked List in C++.
- Inserting Elements to a doubly linked list using Javascript
- Removing Elements from a Double Linked List using Javascript
- C++ Pairwise Swap Elements of a Given Linked List
- Count rotations in sorted and rotated linked list in C++