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

 Live Demo

#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

Advertisements