Maximum occurring character in a linked list

Given a linked list of characters. The task is to find the maximum occurring character in the linked list. if there are multiple answers, return the first maximum occurring character.

Examples:

Input  : g -> e -> e -> k -> s
Output : e

Input  : a -> a -> b -> b -> c -> c -> d -> d
Output : d

Method 1:
Iteratively count the frequency of each character in a string and return the one which has maximum occurrence.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count the maximum
// occurring character in linked list
#include <bits/stdc++.h>
using namespace std;
  
/* Link list node */
struct Node {
  char data;
  struct Node *next;
};
  
char maxChar(struct Node *head) {
  struct Node *p = head;
  
  int max = -1;
  char res;
  
  while (p != NULL) {
  
    // counting the frequency of current element
    // p->data
    struct Node *q = p->next;
    int count = 1;
    while (q != NULL) {
      if (p->data == q->data)
        count++;
  
      q = q->next;
    }
  
    // if current counting is greater than max
    if (max < count) {
      res = p->data;
      max = count;
    }
  
    p = p->next;
  }
  
  return res;
}
  
/* Push a node to linked list. Note that
   this function changes the head */
void push(struct Node **head_ref, char new_data) {
  struct Node *new_node = new Node;
  new_node->data = new_data;
  new_node->next = (*head_ref);
  (*head_ref) = new_node;
}
  
/* Drier program to test above function*/
int main() {
  /* Start with the empty list */
  struct Node *head = NULL;
  char str[] = "skeegforskeeg";
  int i;
  
  // this will create a linked list of
  // character "geeksforgeeks"
  for (i = 0; str[i] != '\0'; i++)
    push(&head, str[i]);
  
  cout << maxChar(head);
  
  return 0;
}

chevron_right


Output:

e

Time complexityO(N*N)

Method 2: (use count array)
Create a count array and count each character frequency return the maximum occurring character.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count the maximum
// occurring character in linked list
#include <bits/stdc++.h>
using namespace std;
  
/* Link list node */
struct Node {
  char data;
  struct Node *next;
};
  
char maxChar(struct Node *head) {
  struct Node *p = head;
  int hash[256] = {0};
  
  // Storing element's frequencies
  // in a hash table.
  while (p != NULL) {
    hash[p->data]++;
    p = p->next;
  }
  
  p = head;
  
  int max = -1;
  char res;
  
  // calculating the first maximum element
  while (p != NULL) {
    if (max < hash[p->data]) {
      res = p->data;
      max = hash[p->data];
    }
    p = p->next;
  }
  return res;
}
  
/* Push a node to linked list. Note that
   this function changes the head */
void push(struct Node **head_ref, char new_data) {
  struct Node *new_node = new Node;
  new_node->data = new_data;
  new_node->next = (*head_ref);
  (*head_ref) = new_node;
}
  
/* Drier program to test above function*/
int main() {
  struct Node *head = NULL;
  char str[] = "skeegforskeeg";
  for (int i = 0; str[i] != '\0'; i++)
    push(&head, str[i]);
  cout << maxChar(head);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count the maximum
// occurring character in linked list
import java.util.*;
  
class GFG 
{
  
/* Link list node */
static class Node 
{
    char data;
    Node next;
};
  
static Node head;
static char maxChar(Node head)
{
    Node p = head;
    int []hash = new int[256];
      
    // Storing element's frequencies
    // in a hash table.
    while (p != null
    {
        hash[p.data]++;
        p = p.next;
    }
      
    p = head;
      
    int max = -1;
    char res = 0;
      
    // calculating the first maximum element
    while (p != null)
    {
        if (max < hash[p.data])
        {
            res = p.data;
            max = hash[p.data];
        }
        p = p.next;
    }
    return res;
}
      
/* Push a node to linked list. Note that
this function changes the head */
static void push(Node head_ref, char new_data) 
{
    Node new_node = new Node();
    new_node.data = new_data;
    new_node.next = head_ref;
    head_ref = new_node;
    head = head_ref;
}
  
// Driver Code
public static void main(String[] args)
{
    head = null;
    char str[] = "skeegforskeeg".toCharArray();
    for (int i = 0; i < str.length; i++) 
    {
        push(head, str[i]);
    }
    System.out.println(maxChar(head));
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count the maximum
// occurring character in linked list
using System;
      
public class GFG 
{
   
/* Link list node */
class Node 
{
    public char data;
    public Node next;
};
   
static Node head;
static char maxChar(Node head)
{
    Node p = head;
    int []hash = new int[256];
       
    // Storing element's frequencies
    // in a hash table.
    while (p != null
    {
        hash[p.data]++;
        p = p.next;
    }
       
    p = head;
       
    int max = -1;
    char res= '\x0000';
       
    // calculating the first maximum element
    while (p != null)
    {
        if (max < hash[p.data])
        {
            res = p.data;
            max = hash[p.data];
        }
        p = p.next;
    }
    return res;
}
       
/* Push a node to linked list. Note that
this function changes the head */
static void push(Node head_ref, char new_data) 
{
    Node new_node = new Node();
    new_node.data = new_data;
    new_node.next = head_ref;
    head_ref = new_node;
    head = head_ref;
}
   
// Driver Code
public static void Main(String[] args)
{
    head = null;
    char []str = "skeegforskeeg".ToCharArray();
    for (int i = 0; i < str.Length; i++) 
    {
        push(head, str[i]);
    }
    Console.WriteLine(maxChar(head));
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

e

Time complexityO(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, 29AjayKumar