Find the Highest Occurring Digit in a Linked List Nodes
Last Updated :
03 Dec, 2023
Given a linked list, the task is to find the highest occurring digit in the linked list. Each node in the linked list contains a positive integer value. Analyze the digits present in these values and determine the digit that occurs the most frequently throughout the linked list.
Examples:
Input: 2 -> 11 -> 3 -> 2 -> 1
Output: 1
Explanation: In this example, the linked list is: 2 -> 11 -> 3 -> 2 -> 1. The digit 2 occurs twice, the digit 1 occurs thrice, and the digit 3 also occurs once. The digit 2 occurs in two nodes: the first node (2) and the fourth node (2). The digit 1 occurs in two nodes: the second node contains two times (11) and the fifth node (1). The digit 3 occurs in one node: the third node (3). Among these digits, the digit 1 has the highest occurrence with a count of 3. Therefore, the output is 1.
Input: 5 -> 5 -> 3 -> 2 -> 12
Output: 5
Explanation: In this example, the linked list is: 5 -> 5 -> 3 -> 2 -> 12. The digit 5 occurs twice, the digit 3 occurs once, and the digit 2 also occurs twice. The digit 5 occurs in two nodes: the first node (5) and the second node (5). The digit 3 occurs in one node : the third node (3). The digit 2 occurs in two node: the fourth node (2) and in the fifth node (2). Among these digits, the digit 5 and 2 has the highest occurrence with a count of 2 . Therefore, the output is 5 because 5 digit occurs before digit 2.
Approach: To solve the problem follow the below idea:
The intuition behind the approach is to iterate through the linked list and examine each node’s value. By extracting individual digits from the value, we can keep track of their occurrences using an unordered map. The algorithm maintains a count for each digit encountered, updating the count whenever a digit is encountered again. Throughout the traversal, it identifies the digit with the highest count. Finally, it returns the digit that occurs the most frequently in the linked list. This approach efficiently determines the highest occurring digit by analyzing the digits within the linked list nodes and selecting the one with the maximum count.
Below are the steps of the approach:
- Initialize variables: maxCount and digitWithMaxCount.
- Start traversing the linked list from the head.
- For each node, extract its value.
- Count how many times each digit appears in the value.
- Update maxCount and digitWithMaxCount if a digit appears more times.
- Move to the next node and repeat steps 3-5.
- After traversing all nodes, return the digit with the highest count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int highestOccurringDigit(Node* head)
{
unordered_map< int , int > digitCount;
int maxCount = 0;
int digitWithMaxCount = 0;
Node* current = head;
while (current != NULL) {
int num = current->data;
while (num != 0) {
int digit = num % 10;
digitCount[digit]++;
if (digitCount[digit] > maxCount
|| (digitCount[digit] == maxCount
&& digit > digitWithMaxCount)) {
maxCount = digitCount[digit];
digitWithMaxCount = digit;
}
num /= 10;
}
current = current->next;
}
return digitWithMaxCount;
}
Node* createNode( int data)
{
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main()
{
Node* head = createNode(2);
head->next = createNode(11);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(1);
int highestDigit = highestOccurringDigit(head);
cout << "Highest Occurring Digit: " << highestDigit
<< endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class Node {
int data;
Node next;
Node( int data) {
this .data = data;
this .next = null ;
}
}
public class Main {
static int highestOccurringDigit(Node head) {
Map<Integer, Integer> digitCount = new HashMap<>();
int maxCount = 0 ;
int digitWithMaxCount = 0 ;
Node current = head;
while (current != null ) {
int num = current.data;
while (num != 0 ) {
int digit = num % 10 ;
digitCount.put(digit, digitCount.getOrDefault(digit, 0 ) + 1 );
if (digitCount.get(digit) > maxCount ||
(digitCount.get(digit) == maxCount && digit > digitWithMaxCount)) {
maxCount = digitCount.get(digit);
digitWithMaxCount = digit;
}
num /= 10 ;
}
current = current.next;
}
return digitWithMaxCount;
}
static Node createNode( int data) {
return new Node(data);
}
public static void main(String[] args) {
Node head = createNode( 2 );
head.next = createNode( 11 );
head.next.next = createNode( 3 );
head.next.next.next = createNode( 2 );
head.next.next.next.next = createNode( 1 );
int highestDigit = highestOccurringDigit(head);
System.out.println( "Highest Occurring Digit: " + highestDigit);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def highest_occuring_digit(head):
digit_count = {}
max_count = 0
digit_with_max_count = 0
current = head
while current is not None :
num = current.data
while num ! = 0 :
digit = num % 10
digit_count[digit] = digit_count.get(digit, 0 ) + 1
if digit_count[digit] > max_count or (digit_count[digit] = = max_count and digit > digit_with_max_count):
max_count = digit_count[digit]
digit_with_max_count = digit
num / / = 10
current = current. next
return digit_with_max_count
def create_node(data):
return Node(data)
if __name__ = = "__main__" :
head = create_node( 2 )
head. next = create_node( 11 )
head. next . next = create_node( 3 )
head. next . next . next = create_node( 2 )
head. next . next . next . next = create_node( 1 )
highest_digit = highest_occuring_digit(head)
print (f "Highest Occurring Digit: {highest_digit}" )
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int Data { get ; set ; }
public Node Next { get ; set ; }
public Node( int data)
{
Data = data;
Next = null ;
}
}
public class MainClass
{
static int HighestOccurringDigit(Node head)
{
Dictionary< int , int > digitCount = new Dictionary< int , int >();
int maxCount = 0;
int digitWithMaxCount = 0;
Node current = head;
while (current != null )
{
int num = current.Data;
while (num != 0)
{
int digit = num % 10;
digitCount[digit] = digitCount.TryGetValue(digit, out int count) ? count + 1 : 1;
if (digitCount[digit] > maxCount ||
(digitCount[digit] == maxCount && digit > digitWithMaxCount))
{
maxCount = digitCount[digit];
digitWithMaxCount = digit;
}
num /= 10;
}
current = current.Next;
}
return digitWithMaxCount;
}
static Node CreateNode( int data)
{
return new Node(data);
}
public static void Main( string [] args)
{
Node head = CreateNode(2);
head.Next = CreateNode(11);
head.Next.Next = CreateNode(3);
head.Next.Next.Next = CreateNode(2);
head.Next.Next.Next.Next = CreateNode(1);
int highestDigit = HighestOccurringDigit(head);
Console.WriteLine( "Highest Occurring Digit: " + highestDigit);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function highestOccurringDigit(head) {
const digitCount = new Map();
let maxCount = 0;
let digitWithMaxCount = 0;
let current = head;
while (current !== null ) {
let num = current.data;
while (num !== 0) {
const digit = num % 10;
digitCount.set(digit, (digitCount.get(digit) || 0) + 1);
if (digitCount.get(digit) > maxCount || (digitCount.get(digit) === maxCount && digit > digitWithMaxCount)) {
maxCount = digitCount.get(digit);
digitWithMaxCount = digit;
}
num = Math.floor(num / 10);
}
current = current.next;
}
return digitWithMaxCount;
}
function createNode(data) {
return new Node(data);
}
const head = createNode(2);
head.next = createNode(11);
head.next.next = createNode(3);
head.next.next.next = createNode(2);
head.next.next.next.next = createNode(1);
const highestDigit = highestOccurringDigit(head);
console.log( "Highest Occurring Digit: " + highestDigit);
|
Output
Highest Occurring Digit: 1
Time Complexity: O(N), where N is the number of nodes in the linked list.
Axillary Space: O(1) (constant) because the maximum number of entries in the unordered map remains constant regardless of the size of the linked list.
Share your thoughts in the comments
Please Login to comment...