Posted on by Kalkicode
Code Hash

Find most occurring node in linked list

Here given code implementation process.

/*
  Java Program 
  Find most occurring node in linked list
*/
import java.util.HashMap;

// Linked list node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
    }
}
public class SingleLL
{
    public LinkNode head;
    public LinkNode tail;
    public SingleLL()
    {
        this.head = null;
        this.tail = null;
    }
   
    // Add new Node at end of linked list 
    public void addNode(int data)
    {
        LinkNode node = new LinkNode(data);
        if (this.head == null)
        {
            this.head = node;
        }
        else
        {
            // Append the node at last position
            this.tail.next = node;
        }
        this.tail = node;
    }
    
    // Display linked list element
    public void display()
    {
        LinkNode temp = this.head;

        //iterating linked list elements
        while (temp != null)
        {
            System.out.print(" " + temp.data + " →");
            // Visit to next node
            temp = temp.next;
        }
        System.out.print(" NULL\n");
    }
    
     // Print most occurrence nodes
    public void mostOccurrence()
    {
        System.out.print("\n Linked List \n");
        this.display();

        LinkNode temp = this.head;
        // Use to collect element occurrence
        HashMap < Integer, Integer > record = new HashMap < Integer, Integer > ();
       
        int max = 1;
        //iterating linked list elements
        while (temp != null)
        {
            if (record.containsKey(temp.data))
            {
                record.put(temp.data,record.get(temp.data)+1);

                if(max < record.get(temp.data))
                {
                    max = record.get(temp.data);
                }
            }
            else
            {
                record.put(temp.data, 1);
            }
            // Visit to next node
            temp = temp.next;
        }

        System.out.print(" Result : [");
        // Print resultant nodes
        for (int key: record.keySet())
        {
            if (record.get(key)== max)
            {
                System.out.print(" "+key);
            }
        }
        System.out.print(" ]");

    }
    public static void main(String[] args)
    {
        SingleLL sll1 = new SingleLL();
        SingleLL sll2 = new SingleLL();
        // First linked list
        //  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
        sll1.addNode(3);
        sll1.addNode(-2);
        sll1.addNode(8);
        sll1.addNode(3);
        sll1.addNode(-2);
        sll1.addNode(16);
        sll1.addNode(3);
        sll1.addNode(-2);
        sll1.mostOccurrence();
        // Second linked list
        //  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
        sll2.addNode(4);
        sll2.addNode(9);
        sll2.addNode(7);
        sll2.addNode(4);
        sll2.addNode(8);
        sll2.addNode(9);
        sll2.addNode(-2);
        sll2.addNode(9);
        sll2.mostOccurrence();
    }
}

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ -2 3 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
/*
  C++ Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode
{
	public: 
    int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: 
    LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	// Add new Node at end of linked list
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// Print most occurrence nodes
	void mostOccurrence()
	{
		cout << "\n Linked List \n";
		this->display();
		LinkNode *temp = this->head;
		// Use to collect element occurrence
		unordered_map < int, int > record;
		int max = 1;
		//iterating linked list elements
		while (temp != NULL)
		{
			if (record.find(temp->data) != record.end())
			{
				record[temp->data] = record[temp->data] + 1;
				if (max < record[temp->data])
				{
					max = record[temp->data];
				}
			}
			else
			{
				record[temp->data] = 1;
			}
			// Visit to next node
			temp = temp->next;
		}
		cout << " Result : [";
		for (auto &info: record)
		{
			if (info.second == max)
			{
				cout << " " << info.first ;
			}
		}
		cout << " ]";
	}
};
int main()
{
	SingleLL sll1 = SingleLL();
	SingleLL sll2 = SingleLL();
	// First linked list
	//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(8);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(16);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.mostOccurrence();
	// Second linked list
	//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(4);
	sll2.addNode(8);
	sll2.addNode(9);
	sll2.addNode(-2);
	sll2.addNode(9);
	sll2.mostOccurrence();
	return 0;
}

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ -2 3 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
// Include namespace system
using System;
using System.Collections.Generic;
/*
  C# Program 
  Find most occurring node in linked list
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" NULL\n");
	}
	// Print most occurrence nodes
	public void mostOccurrence()
	{
		Console.Write("\n Linked List \n");
		this.display();
		LinkNode temp = this.head;
		// Use to collect element occurrence
		Dictionary < int, int > record = new Dictionary < int, int > ();
		int max = 1;
		//iterating linked list elements
		while (temp != null)
		{
			if (record.ContainsKey(temp.data))
			{
                // increase frequency
				record[temp.data] = record[temp.data] + 1;
				if (max < record[temp.data])
				{
					max = record[temp.data];
				}
			}
			else
			{
				record.Add(temp.data, 1);
			}
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" Result : [");
		foreach(KeyValuePair < int, int > info in record)
		{
			if (info.Value == max)
			{
				Console.Write(" " + info.Key);
			}
		}
		Console.Write(" ]");
	}
	public static void Main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		// First linked list
		//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.addNode(8);
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.addNode(16);
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.mostOccurrence();
		// Second linked list
		//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(4);
		sll2.addNode(8);
		sll2.addNode(9);
		sll2.addNode(-2);
		sll2.addNode(9);
		sll2.mostOccurrence();
	}
}

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ 3 -2 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
<?php
/*
  Php Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
}
class SingleLL
{
	public $head;
	public $tail;

	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	// Add new Node at end of linked list
	public function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public function display()
	{
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			echo " ". $temp->data ." →";
			// Visit to next node
			$temp = $temp->next;
		}
		echo " NULL\n";
	}
	// Print most occurrence nodes
	public function mostOccurrence()
	{
		echo "\n Linked List \n";
		$this->display();
		$temp = $this->head;
		// Use to collect element occurrence
		$record = array();
		$max = 1;
		//iterating linked list elements
		while ($temp != null)
		{
			if (array_key_exists($temp->data, $record))
			{
				$record[$temp->data] = $record[$temp->data] + 1;
				if ($max < $record[$temp->data])
				{
					$max = $record[$temp->data];
				}
			}
			else
			{
				$record[$temp->data] = 1;
			}
			// Visit to next node
			$temp = $temp->next;
		}
		echo " Result : [";
		foreach($record as $key => $value)
		{
			if ($value == $max)
			{
				echo " ".$key;
			}
		}
		echo " ]";
	}
}

function main()
{
	$sll1 = new SingleLL();
	$sll2 = new SingleLL();
	// First linked list
	//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	$sll1->addNode(3);
	$sll1->addNode(-2);
	$sll1->addNode(8);
	$sll1->addNode(3);
	$sll1->addNode(-2);
	$sll1->addNode(16);
	$sll1->addNode(3);
	$sll1->addNode(-2);
	$sll1->mostOccurrence();
	// Second linked list
	//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	$sll2->addNode(4);
	$sll2->addNode(9);
	$sll2->addNode(7);
	$sll2->addNode(4);
	$sll2->addNode(8);
	$sll2->addNode(9);
	$sll2->addNode(-2);
	$sll2->addNode(9);
	$sll2->mostOccurrence();
}
main();

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ 3 -2 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
/*
  Node Js Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	// Print most occurrence nodes
	mostOccurrence()
	{
		process.stdout.write("\n Linked List \n");
		this.display();
		var temp = this.head;
		// Use to collect element occurrence
		var record = new Map();
		var max = 1;
		//iterating linked list elements
		while (temp != null)
		{
			if (record.has(temp.data))
			{
				record.set(temp.data, record.get(temp.data) + 1);
				if (max < record.get(temp.data))
				{
					max = record.get(temp.data);
				}
			}
			else
			{
				record.set(temp.data, 1);
			}
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" Result : [");
		for (let [key, value] of record)
		{
			if (record.get(key) == max)
			{
				process.stdout.write(" " + key);
			}
		}
		process.stdout.write(" ]");
	}
}

function main()
{
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	// First linked list
	//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(8);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(16);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.mostOccurrence();
	// Second linked list
	//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(4);
	sll2.addNode(8);
	sll2.addNode(9);
	sll2.addNode(-2);
	sll2.addNode(9);
	sll2.mostOccurrence();
}
main();

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ 3 -2 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
#   Python 3 Program 
#   Find most occurring node in linked list

#  Linked list node
class LinkNode :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  Print most occurrence nodes
	def mostOccurrence(self) :
		print("\n Linked List ")
		self.display()
		temp = self.head
		#  Use to collect element occurrence
		record = dict()
		max = 1
		# iterating linked list elements
		while (temp != None) :
			if (temp.data in record.keys()) :
				record[temp.data] = record.get(temp.data) + 1
				if (max < record.get(temp.data)) :
					max = record.get(temp.data)
				
			else :
				record[temp.data] = 1
			
			#  Visit to next node
			temp = temp.next
		
		print(" Result : [", end = "")
		for key, value in record.items() :
			if (value == max) :
				print(" ", key, end = "")
			
		
		print(" ]", end = "")
	

def main() :
	sll1 = SingleLL()
	sll2 = SingleLL()
	#  First linked list
	#   3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	sll1.addNode(3)
	sll1.addNode(-2)
	sll1.addNode(8)
	sll1.addNode(3)
	sll1.addNode(-2)
	sll1.addNode(16)
	sll1.addNode(3)
	sll1.addNode(-2)
	sll1.mostOccurrence()
	#  Second linked list
	#   4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	sll2.addNode(4)
	sll2.addNode(9)
	sll2.addNode(7)
	sll2.addNode(4)
	sll2.addNode(8)
	sll2.addNode(9)
	sll2.addNode(-2)
	sll2.addNode(9)
	sll2.mostOccurrence()

if __name__ == "__main__": main()

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [  3  -2 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [  9 ]
#   Ruby Program 
#   Find most occurring node in linked list

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL 
	# Define the accessor and reader of class SingleLL
	attr_reader :head, :tail
	attr_accessor :head, :tail
 
	
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	#  Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

	#  Display linked list element
	def display() 
		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL\n")
	end

	#  Print most occurrence nodes
	def mostOccurrence() 
		print("\n Linked List \n")
		self.display()
		temp = self.head
		#  Use to collect element occurrence
		record = Hash.new
		max = 1
		# iterating linked list elements
		while (temp != nil) 
			if (record.key?(temp.data)) 
				record[temp.data] = record[temp.data] + 1
				if (max < record[temp.data]) 
					max = record[temp.data]
				end

			else 
				record[temp.data] = 1
			end

			#  Visit to next node
			temp = temp.next
		end

		print(" Result : [")
		record.each { | key, value | 
			if (record[key] == max) 
				print(" ", key)
			end
		}
		print(" ]")
	end

end

def main() 
		sll1 = SingleLL.new()
		sll2 = SingleLL.new()
		#  First linked list
		#   3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
		sll1.addNode(3)
		sll1.addNode(-2)
		sll1.addNode(8)
		sll1.addNode(3)
		sll1.addNode(-2)
		sll1.addNode(16)
		sll1.addNode(3)
		sll1.addNode(-2)
		sll1.mostOccurrence()
		#  Second linked list
		#   4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
		sll2.addNode(4)
		sll2.addNode(9)
		sll2.addNode(7)
		sll2.addNode(4)
		sll2.addNode(8)
		sll2.addNode(9)
		sll2.addNode(-2)
		sll2.addNode(9)
		sll2.mostOccurrence()
end

main()

Output

 Linked List 
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ 3 -2 ]
 Linked List 
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
import scala.collection.mutable._;
/*
  Scala Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
	def this()
	{
		this(null, null);
	}
	// Add new Node at end of linked list
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Print most occurrence nodes
	def mostOccurrence(): Unit = {
		print("\n Linked List \n");
		this.display();
		var temp: LinkNode = this.head;
		// Use to collect element occurrence
		var record = Map[Int, Int]();
		var max: Int = 1;
		//iterating linked list elements
		while (temp != null)
		{
			if (record.contains(temp.data))
			{
				record.addOne(temp.data, record.get(temp.data).get + 1);
				if (max < record.get(temp.data).get)
				{
					max = record.get(temp.data).get;
				}
			}
			else
			{
				record.addOne(temp.data, 1);
			}
			// Visit to next node
			temp = temp.next;
		}
		print(" Result : [");
		for ((key, value) <- record)
		{
			if (value == max)
			{
				print(" " + key);
			}
		}
		print(" ]");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll1: SingleLL = new SingleLL();
		var sll2: SingleLL = new SingleLL();
		// First linked list
		//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.addNode(8);
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.addNode(16);
		sll1.addNode(3);
		sll1.addNode(-2);
		sll1.mostOccurrence();
		// Second linked list
		//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(4);
		sll2.addNode(8);
		sll2.addNode(9);
		sll2.addNode(-2);
		sll2.addNode(9);
		sll2.mostOccurrence();
	}
}

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ -2 3 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]
import Foundation
/*
  Swift 4 Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		var temp: LinkNode? = self.head;
		//iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// Print most occurrence nodes
	func mostOccurrence()
	{
		print("\n Linked List ");
		self.display();
		var temp: LinkNode? = self.head;
		// Use to collect element occurrence
		var record = [Int: Int]();
		var max: Int = 1;
		//iterating linked list elements
		while (temp  != nil)
		{
			if (record.keys.contains(temp!.data))
			{
				record[temp!.data] = record[temp!.data]! + 1;
				if (max < record[temp!.data]!)
				{
					max = record[temp!.data]!;
				}
			}
			else
			{
				record[temp!.data] = 1;
			}
			// Visit to next node
			temp = temp!.next;
		}
		print(" Result : [", terminator: "");
		for (key, value) in record
		{
			if (value == max)
			{
				print(" ", key, terminator: "");
			}
		}
		print(" ]", terminator: "");
	}
}
func main()
{
	let sll1: SingleLL = SingleLL();
	let sll2: SingleLL = SingleLL();
	// First linked list
	//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(8);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(16);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.mostOccurrence();
	// Second linked list
	//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(4);
	sll2.addNode(8);
	sll2.addNode(9);
	sll2.addNode(-2);
	sll2.addNode(9);
	sll2.mostOccurrence()
}
main();

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [  -2  3 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [  9 ]
/*
  Kotlin Program 
  Find most occurring node in linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list
	fun addNode(data: Int): Unit
	{
		var node: LinkNode = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		var temp: LinkNode ? = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Print most occurrence nodes
	fun mostOccurrence(): Unit
	{
		print("\n Linked List \n");
		this.display();
		var temp: LinkNode ? = this.head;
		// Use to collect element occurrence
		var record = mutableMapOf < Int , Int > ();
		var max: Int = 1;
		//iterating linked list elements
		while (temp != null)
		{
			if (record.containsKey(temp.data))
			{
				record.put(temp.data, record.getValue(temp.data) + 1);
				if (max < record.getValue(temp.data))
				{
					max = record.getValue(temp.data);
				}
			}
			else
			{
				record.put(temp.data, 1);
			}
			// Visit to next node
			temp = temp.next;
		}
		print(" Result : [");
		// Print resultant nodes
		for (key in record.keys)
		{
			if (record.getValue(key) == max)
			{
				print(" " + key);
			}
		}
		print(" ]");
	}
}
fun main(args: Array < String > ): Unit
{
	var sll1: SingleLL = SingleLL();
	var sll2: SingleLL = SingleLL();
	// First linked list
	//  3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(8);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.addNode(16);
	sll1.addNode(3);
	sll1.addNode(-2);
	sll1.mostOccurrence();
	// Second linked list
	//  4 → 9 → 7 → 4 → 8 → 9 → -2 → NULL
	sll2.addNode(4);
	sll2.addNode(9);
	sll2.addNode(7);
	sll2.addNode(4);
	sll2.addNode(8);
	sll2.addNode(9);
	sll2.addNode(-2);
	sll2.addNode(9);
	sll2.mostOccurrence();
}

Output

 Linked List
 3 → -2 → 8 → 3 → -2 → 16 → 3 → -2 → NULL
 Result : [ 3 -2 ]
 Linked List
 4 → 9 → 7 → 4 → 8 → 9 → -2 → 9 → NULL
 Result : [ 9 ]

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment