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// frequency elements in Linked list#include <bits/stdc++.h>usingnamespacestd;/* Link list node */structNode {    intkey;    structNode* 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. */voidpush(structNode** head_ref, intnew_key){    structNode* new_node = newNode;    new_node->key = new_key;    new_node->next = (*head_ref);    (*head_ref) = new_node;}// Function to count minimum frequency elements// in the linked listintcountMinimum(structNode* head){    // Store frequencies of all nodes.    unordered_map<int, int> mp;    structNode* current = head;    while(current != NULL) {        intdata = current->key;        mp[data]++;        current = current->next;    }    // Find min frequency    current = head;    intmin_frequency = INT_MAX, countMin = 0;    for(autoit = mp.begin(); it != mp.end(); it++) {        if(it->second <= min_frequency) {            min_frequency = it->second;        }    }    // Find count of min frequency elements    for(autoit = mp.begin(); it != mp.end(); it++) {        if(it->second == min_frequency) {            countMin += (it->second);        }    }    returncountMin;}/* Driver program to test count function*/intmain(){    /* Start with the empty list */    structNode* head = NULL;    intx = 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;    return0;} | 
Java
| // Java program to find count of minimum// frequency elements in Linked listimportjava.util.*;classGFG{/* Link list node */staticclassNode {    intkey;    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. */staticNode push(Node head_ref, intnew_key){    Node new_node = newNode();    new_node.key = new_key;    new_node.next = (head_ref);    (head_ref) = new_node;    returnhead_ref;}// Function to count minimum frequency elements// in the linked liststaticintcountMinimum( Node head){    // Store frequencies of all nodes.    HashMap<Integer,Integer> mp = newHashMap<Integer,Integer>();    Node current = head;    while(current != null)    {        intdata = current.key;        mp.put(data, (mp.get(data) == null? 1:mp.get(data) + 1));        current = current.next;    }    // Find min frequency    current = head;    intmin_frequency = Integer.MAX_VALUE, countMin = 0;    for(Map.Entry<Integer,Integer> it :mp.entrySet())     {        if(it.getValue() <= min_frequency)        {            min_frequency = it.getValue();        }    }    // Find count of min frequency elements    for(Map.Entry<Integer,Integer> it :mp.entrySet())     {        if(it.getValue() == min_frequency)        {            countMin += (it.getValue());        }    }    returncountMin;}/* Driver code*/publicstaticvoidmain(String args[]){    /* Start with the empty list */    Node head = null;    intx = 21;    /* 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);    System.out.println( countMinimum(head) );}}// This code is contributed by andrew1234 | 
Python3
| # Python3 program to find count of minimum # frequency elements in Linked list importsysimportmath# Link list nodeclassNode:    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. defpush(head,data):    ifnothead:        returnNode(data)    temp =Node(data)    temp.next=head    head =temp    returnhead# Function to count minimum frequency elements # in the linked list defcountMinimun(head):        # Store frequencies of all nodes.     freq ={}    temp =head    while(temp):        d =temp.data        ifd infreq:            freq[d] =freq.get(d) +1        else:            freq[d] =1        temp =temp.next        # Find min frequency    minimum_freq =sys.maxsize    fori infreq:        minimum_freq =min(minimum_freq, freq.get(i))        # Find count of min frequency elements     countMin =0    fori infreq:        iffreq.get(i) ==minimum_freq:            countMin +=1    returncountMin# 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 Contributed by Vikash Kumar 37 | 
C#
| // C# program to find count of minimum// frequency elements in Linked listusingSystem;usingSystem.Collections.Generic;classGFG{/* Link list node */publicclassNode {    publicintkey;    publicNode 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. */staticNode push(Node head_ref, intnew_key){    Node new_node = newNode();    new_node.key = new_key;    new_node.next = (head_ref);    head_ref = new_node;    returnhead_ref;}// Function to count minimum frequency elements// in the linked liststaticintcountMinimum( Node head){    // Store frequencies of all nodes.    IDictionary<int,int> mp = newDictionary<int,int>();        Node current = head;    while(current != null)    {        intdata = current.key;        intval = 0;        mp[data] = (!mp.TryGetValue(data,outval)  ? 1:mp[data]+ 1);        current = current.next;    }    // Find min frequency    current = head;    intmin_frequency = int.MaxValue, countMin = 0;    foreach(KeyValuePair<int, int> it inmp)     {        if(it.Value <= min_frequency)        {            min_frequency = it.Value;        }    }    // Find count of min frequency elements    foreach(KeyValuePair<int, int> it inmp)     {        if(it.Value == min_frequency)        {            countMin += (it.Value);        }    }    returncountMin;}/* Driver code*/publicstaticvoidMain(){    /* Start with the empty list */    Node head = null;    intx = 21;    /* 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);    Console.WriteLine( countMinimum(head) );}}// This code is contributed by SoumikMondal | 
Javascript
| <script>      // JavaScript program to find count of minimum      // frequency elements in Linked list      /* Link list node */      class Node {        constructor() {          this.key = 0;          this.next = null;        }      }      /* Given a reference (pointer to pointer) to the headof a list and an int, push a new node on the frontof the list. */      functionpush(head_ref, new_key) {        varnew_node = newNode();        new_node.key = new_key;        new_node.next = head_ref;        head_ref = new_node;        returnhead_ref;      }      // Function to count minimum frequency elements      // in the linked list      functioncountMinimum(head) {        // Store frequencies of all nodes.        varmp = {};        varcurrent = head;        while(current != null) {          vardata = current.key;          varval = 0;          mp[data] = !mp.hasOwnProperty(data) ? 1 : mp[data] + 1;          current = current.next;        }        // Find min frequency        current = head;        varmin_frequency = 2147483647,          countMin = 0;        for(const [key, value] of Object.entries(mp)) {          if(value <= min_frequency) {            min_frequency = value;          }        }        // Find count of min frequency elements        for(const [key, value] of Object.entries(mp)) {          if(value == min_frequency) {            countMin += value;          }        }        returncountMin;      }      /* Driver code*/      /* Start with the empty list */      varhead = null;      varx = 21;      /* 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);      document.write(countMinimum(head) + "<br>");            // This code is contributed by rdtank.    </script> | 
2
Time Complexity : O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







