Maximum Occurring Character in a Linked List


We have given a singly linked list of characters, and our task is to print the character which occurs the maximum time in the linked list. If the multiple characters have the same count of occurring then print the character which comes in the last.

The Singly-linked list is a linear data structure that consists of nodes. Each node contains the data and the pointer to the next node which contains the memory address of the next node because the memory assigned to each node is not continuous.

Sample Examples

Let us assume we have given a linked list of characters as

Example 1

Input: LL = a -> b -> c -> c -> c

Output: The maximum occurring character is c.

Explanation: In the given linked list LL a comes 1 time, b comes 1 time, and c comes 3 times. Therefore, the output is c.

Example 2

Input:

LL = x -> x -> y -> y -> z -> z

Output: The maximum occurring character is z.

Explanation: In the given linked list LL, x comes 2 times, y comes 2 times and z comes 2 times. All have the same number of occurrences as z comes in the last therefore the output is z.

Here we are going to discuss two approaches. Let’s see them below section-

Approach 1: Iteratively count frequency

The idea of this approach is that we are going to traverse the linked list and count the frequency of each character and then find out the character which has the maximum frequency and print the character if more than one character has the same frequency then return the last character.

Example

#include <iostream>
using namespace std;
// creating a class to have a structure for linked list nodes 
class Node{
   public:
   char data; // variable to store the characters
   Node* next = NULL; // variable to store the address of the next node     
   Node(char cur){
      data = cur;
   }
};
// function to print the elements of the linked list 
void printLL(Node* head){   
   // creating a temporary pointer 
   Node* temp = head;    
   while(temp != nullptr){
      cout<<temp->data<<" -> ";
      temp = temp->next;
   }
   cout<<"NULL"<<endl;
}
// function to find the max frequency 
void maxFreq(Node* head){
   // traversing over the linked list for each character 
   // starting from the first character to the last character     
   int ans = 0; // variable to store the maximum frequency 
   char char_ans;
   Node* temp_first = head; // variable to store the current first node     
   while(temp_first != nullptr){
      int cur = 0; // variable to store the frequency of the current character 
      Node* temp = temp_first;        
      while(temp != nullptr){
         if(temp->data == temp_first->data){
            cur++;
         }
         temp = temp->next;
      }
      if(ans < cur){
         ans = cur;
         char_ans = temp_first->data;
      }
      temp_first = temp_first->next;
   }
   cout<<"The last character in the given linked list is '"<<char_ans<<"' with the frequency of "<< ans<<endl;
}
// main function 
int main(){
   // defining the linked list 
   Node* head = new Node('a');
   head->next = new Node('b');
   head->next->next = new Node('b');
   head->next->next->next = new Node('c');
   head->next->next->next->next = new Node('d');
   head->next->next->next->next->next = new Node('d');
   head->next->next->next->next->next->next = new Node('d');  
   cout<<"The given linked list is: "<<endl;
   printLL(head);   
   maxFreq(head);
   return 0;
}

Output

The given linked list is: 
a -> b -> b -> c -> d -> d -> d -> NULL
The last character in the given linked list is 'd' with the frequency of 3

Time Complexity

:O(N*N), Where N is the size of the linked list.

Space Complexity: O(1)

Approach 2: By using a count array

The idea of this approach is that we are going to maintain the count array in which we store the frequency of each character then traverse that array and find the character which has the maximum frequency. Print the character if more than one character has the same frequency then return the last character.

Example

#include <iostream>
using namespace std;
// creating a class to have a structure for linked list nodes 
class Node{
   public:
   char data; // variable to store the characters
   Node* next = NULL; // variable to store the address of the next node     
   Node(char cur){
      data = cur;
   }
};
// function to print the elements of the linked list 
void printLL(Node* head){    
   // creating a temporary pointer 
   Node* temp = head;    
   while(temp != nullptr){
      cout<<temp->data<<" -> ";
      temp = temp->next;
   }
   cout<<"NULL"<<endl;
}
// function to find the max frequency 
void maxFreq(Node* head){
   int ans = 0; // variable to store the maximum frequency 
   char char_ans;    
   // traversing over the linked list for each lowercase character 
   for(char i = 'a'; i<= 'z'; i++){
      Node* temp = head;
      int cur = 0; // variable to store the frequency of the current character 
      while(temp != nullptr){
         if(temp->data == i){
            cur++;
         }
         temp = temp->next;
      }
      if(ans <= cur){
         ans = cur;
         char_ans = i;
      }
   }     
   cout<<"The last character in the given linked list is '"<<char_ans<<"' with the frequency of "<< ans<<endl;
}
int main(){
   // defining the linked list 
   Node* head = new Node('a');
   head->next = new Node('b');
   head->next->next = new Node('b');
   head->next->next->next = new Node('c');
   head->next->next->next->next = new Node('e');
   head->next->next->next->next->next = new Node('d');
   head->next->next->next->next->next->next = new Node('d');  
   cout<<"The given linked list is: "<<endl;
   printLL(head);   
   maxFreq(head);
   return 0;
}

Output

The given linked list is: 
a -> b -> b -> c -> e -> d -> d -> NULL
The last character in the given linked list is 'd' with the frequency of 2

Time Complexity

O(N), Where N is the size of the linked list.

Space Complexity: O(N), where N is the size of the linked list.

Conclusion

Here we have discussed how to find the maximum occurring character in the linked list. For finding the maximum occurring character we have discussed two approaches. First approach using the while loop for each character of the given linked list and second approach using the for loop, for each lowercase character and maintaining the count.

Updated on: 17-May-2023

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements