Posted on by Kalkicode
Code String

Find the second most frequent character in a given string

Here given code implementation process.

/*
    C program for
    Find the second most frequent character in a given string
*/
#include <stdio.h>
#include <string.h>

void secondMostFrequent(char *text)
{
	int n = strlen(text);
	if (n == 0)
	{
		return;
	}
	int first = 0;
	int second = -1;
	int record[256];
	// Set initial count of character record
	for (int i = 0; i < 256; ++i)
	{
		record[i] = 0;
	}
	// Count frequency of character element
	for (int i = 0; i < n; ++i)
	{
		record[text[i]] = record[text[i]] + 1;
	}
	// Find second most occurring character
	for (int i = 0; i < n; ++i)
	{
		if (record[text[i]] > record[text[first]])
		{
			second = first;
			first = i;
		}
		else if (record[text[i]] < record[text[first]])
		{
			if (second == -1 || record[text[second]] < record[text[i]])
			{
				second = i;
			}
		}
	}
	printf("\n Given Text : %s", text);
	if (second == -1)
	{
		printf("\n Second most frequent element not exist");
	}
	else
	{
		printf("\n Second most frequent element is : %c", text[second]);
	}
}
int main(int argc, char
	const *argv[])
{
	// Given Sting text
	char *text1 = "xyoxoxx";
	char *text2 = "abaacdccec";
	char *text3 = "xxyyzz";
	/*
	    Test A
	    ---------
	    text   = "xyoxoxx"
	    "xxxx" = 4
	    "y"    = 1
	    "oo"   = 2
	    -------------------------------
	    o = 2 which is second largest
	*/
	secondMostFrequent(text1);
	/*
	    Test B 
	    ---------
	    text   = "abaacdccec"
	    "aaa"  = 3
	    "b"    = 1
	    "cccc" = 4
	    "d"    = 1
	    -------------------------------
	    a = 3 which is second largest
	*/
	secondMostFrequent(text2);
	/*
	    Test C
	    ---------
	    "xx" = 2
	    "yy" = 2
	    "zz" = 2
	    -------------------------------
	    No second most frequent element
	*/
	secondMostFrequent(text3);
	return 0;
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
/*
    Java program for
    Find the second most frequent character in a given string
*/
public class Occurrence
{
    public void secondMostFrequent(String text)
    {
        int n = text.length();
        if (n == 0)
        {
            return;
        }
        int first = 0;
        int second = -1;
        int[] record = new int[256];
        // Set initial count of character record
        for (int i = 0; i < 256; ++i)
        {
            record[i] = 0;
        }
        // Count frequency of character element
        for (int i = 0; i < n; ++i)
        {
            record[text.charAt(i)] = record[text.charAt(i)] + 1;
        }
        // Find second most occurring character
        for (int i = 0; i < n; ++i)
        {
            if (record[text.charAt(i)] > record[text.charAt(first)])
            {
                second = first;
                first = i;
            }
            else if (record[text.charAt(i)] < record[text.charAt(first)])
            {
                if (second == -1 || 
                    record[text.charAt(second)] < record[text.charAt(i)])
                {
                    second = i;
                }
            }
        }
        System.out.print("\n Given Text : " + text );

        if (second == -1)
        {
            System.out.print("\n Second most frequent element not exist");
        }
        else
        {
            System.out.print("\n Second most frequent element is : " + 
                             text.charAt(second) );
        }
    }
    public static void main(String[] args)
    {
        Occurrence task = new Occurrence();

        // Given Sting text
        String text1 = "xyoxoxx";
        String text2 = "abaacdccec";
        String text3 = "xxyyzz";
        /*
            Test A
            ---------
            text   = "xyoxoxx"
            "xxxx" = 4
            "y"    = 1
            "oo"   = 2
            -------------------------------
            o = 2 which is second largest
        */
        task.secondMostFrequent(text1);
        /*
            Test B 
            ---------
            text   = "abaacdccec"
            "aaa"  = 3
            "b"    = 1
            "cccc" = 4
            "d"    = 1
            -------------------------------
            a = 3 which is second largest
        */
        task.secondMostFrequent(text2);
        /*
            Test C
            ---------
            "xx" = 2
            "yy" = 2
            "zz" = 2
            -------------------------------
            No second most frequent element
        */
        task.secondMostFrequent(text3);

    }
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Find the second most frequent character in a given string
*/
class Occurrence
{
	public: void secondMostFrequent(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		int first = 0;
		int second = -1;
		int record[256];
		// Set initial count of character record
		for (int i = 0; i < 256; ++i)
		{
			record[i] = 0;
		}
		// Count frequency of character element
		for (int i = 0; i < n; ++i)
		{
			record[text[i]] = record[text[i]] + 1;
		}
		// Find second most occurring character
		for (int i = 0; i < n; ++i)
		{
			if (record[text[i]] > record[text[first]])
			{
				second = first;
				first = i;
			}
			else if (record[text[i]] < record[text[first]])
			{
				if (second == -1 || record[text[second]] < record[text[i]])
				{
					second = i;
				}
			}
		}
		cout << "\n Given Text : " << text;
		if (second == -1)
		{
			cout << "\n Second most frequent element not exist";
		}
		else
		{
			cout << "\n Second most frequent element is : " << text[second];
		}
	}
};
int main()
{
	Occurrence *task = new Occurrence();
	// Given Sting text
	string text1 = "xyoxoxx";
	string text2 = "abaacdccec";
	string text3 = "xxyyzz";
	/*
	            Test A
	            ---------
	            text   = "xyoxoxx"
	            "xxxx" = 4
	            "y"    = 1
	            "oo"   = 2
	            -------------------------------
	            o = 2 which is second largest
	        */
	task->secondMostFrequent(text1);
	/*
	            Test B 
	            ---------
	            text   = "abaacdccec"
	            "aaa"  = 3
	            "b"    = 1
	            "cccc" = 4
	            "d"    = 1
	            -------------------------------
	            a = 3 which is second largest
	        */
	task->secondMostFrequent(text2);
	/*
	            Test C
	            ---------
	            "xx" = 2
	            "yy" = 2
	            "zz" = 2
	            -------------------------------
	            No second most frequent element
	        */
	task->secondMostFrequent(text3);
	return 0;
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
// Include namespace system
using System;
/*
    Csharp program for
    Find the second most frequent character in a given string
*/
public class Occurrence
{
	public void secondMostFrequent(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		int first = 0;
		int second = -1;
		int[] record = new int[256];
		// Set initial count of character record
		for (int i = 0; i < 256; ++i)
		{
			record[i] = 0;
		}
		// Count frequency of character element
		for (int i = 0; i < n; ++i)
		{
			record[text[i]] = record[text[i]] + 1;
		}
		// Find second most occurring character
		for (int i = 0; i < n; ++i)
		{
			if (record[text[i]] > record[text[first]])
			{
				second = first;
				first = i;
			}
			else if (record[text[i]] < record[text[first]])
			{
				if (second == -1 || 
                    record[text[second]] < record[text[i]])
				{
					second = i;
				}
			}
		}
		Console.Write("\n Given Text : " + text);
		if (second == -1)
		{
			Console.Write("\n Second most frequent element not exist");
		}
		else
		{
			Console.Write("\n Second most frequent element is : " + text[second]);
		}
	}
	public static void Main(String[] args)
	{
		Occurrence task = new Occurrence();
		// Given Sting text
		String text1 = "xyoxoxx";
		String text2 = "abaacdccec";
		String text3 = "xxyyzz";
		/*
		            Test A
		            ---------
		            text   = "xyoxoxx"
		            "xxxx" = 4
		            "y"    = 1
		            "oo"   = 2
		            -------------------------------
		            o = 2 which is second largest
		        */
		task.secondMostFrequent(text1);
		/*
		            Test B 
		            ---------
		            text   = "abaacdccec"
		            "aaa"  = 3
		            "b"    = 1
		            "cccc" = 4
		            "d"    = 1
		            -------------------------------
		            a = 3 which is second largest
		        */
		task.secondMostFrequent(text2);
		/*
		            Test C
		            ---------
		            "xx" = 2
		            "yy" = 2
		            "zz" = 2
		            -------------------------------
		            No second most frequent element
		        */
		task.secondMostFrequent(text3);
	}
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
package main
import "fmt"
/*
    Go program for
    Find the second most frequent character in a given string
*/

func secondMostFrequent(text string) {
	var n int = len(text)
	if n == 0 {
		return
	}
	var first int = 0
	var second int = -1
	var record = make([]int ,256)
	// Count frequency of character element
	for i := 0 ; i < n ; i++ {
		record[text[i]] = record[text[i]] + 1
	}
	// Find second most occurring character
	for i := 0 ; i < n ; i++ {
		if record[text[i]] > record[text[first]] {
			second = first
			first = i
		} else if record[text[i]] < record[text[first]] {
			if second == -1 || record[text[second]] < record[text[i]] {
				second = i
			}
		}
	}
	fmt.Print("\n Given Text : ", text)
	if second == -1 {
		fmt.Print("\n Second most frequent element not exist")
	} else {
		fmt.Print("\n Second most frequent element is : ", string(text[second]))
	}
}
func main() {

	// Given Sting text
	var text1 string = "xyoxoxx"
	var text2 string = "abaacdccec"
	var text3 string = "xxyyzz"
	/*
	    Test A
	    ---------
	    text   = "xyoxoxx"
	    "xxxx" = 4
	    "y"    = 1
	    "oo"   = 2
	    -------------------------------
	    o = 2 which is second largest
	*/
	secondMostFrequent(text1)
	/*
	    Test B 
	    ---------
	    text   = "abaacdccec"
	    "aaa"  = 3
	    "b"    = 1
	    "cccc" = 4
	    "d"    = 1
	    -------------------------------
	    a = 3 which is second largest
	*/
	secondMostFrequent(text2)
	/*
	    Test C
	    ---------
	    "xx" = 2
	    "yy" = 2
	    "zz" = 2
	    -------------------------------
	    No second most frequent element
	*/
	secondMostFrequent(text3)
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
<?php
/*
    Php program for
    Find the second most frequent character in a given string
*/
class Occurrence
{
    public  function secondMostFrequent($text)
    {
        $n = strlen($text);
        if ($n == 0)
        {
            return;
        }
        $first = 0;
        $second = -1;
        $record = array_fill(0, 256, 0);
        // Count frequency of character element
        for ($i = 0; $i < $n; ++$i)
        {
            $record[ord($text[$i])] = $record[ord($text[$i])] + 1;
        }
        // Find second most occurring character
        for ($i = 0; $i < $n; ++$i)
        {
            if ($record[ord($text[$i])] > $record[ord($text[$first])])
            {
                $second = $first;
                $first = $i;
            }
            else if ($record[ord($text[$i])] < $record[ord($text[$first])])
            {
                if ($second == -1 || 
                    $record[ord($text[$second])] < $record[ord($text[$i])])
                {
                    $second = $i;
                }
            }
        }
        echo("\n Given Text : ".$text);
        if ($second == -1)
        {
            echo("\n Second most frequent element not exist");
        }
        else
        {
            echo("\n Second most frequent element is : ".$text[$second]);
        }
    }
}

function main()
{
    $task = new Occurrence();
    // Given Sting text
    $text1 = "xyoxoxx";
    $text2 = "abaacdccec";
    $text3 = "xxyyzz";
    /*
        Test A
        ---------
        text   = "xyoxoxx"
        "xxxx" = 4
        "y"    = 1
        "oo"   = 2
        -------------------------------
        o = 2 which is second largest
    */
    $task->secondMostFrequent($text1);
    /*
        Test B 
        ---------
        text   = "abaacdccec"
        "aaa"  = 3
        "b"    = 1
        "cccc" = 4
        "d"    = 1
        -------------------------------
        a = 3 which is second largest
    */
    $task->secondMostFrequent($text2);
    /*
        Test C
        ---------
        "xx" = 2
        "yy" = 2
        "zz" = 2
        -------------------------------
        No second most frequent element
    */
    $task->secondMostFrequent($text3);
}
main();

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
/*
    Node JS program for
    Find the second most frequent character in a given string
*/
class Occurrence
{
	secondMostFrequent(text)
	{
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		var first = 0;
		var second = -1;
		var record = Array(256).fill(0);
		// Count frequency of character element
		for (var i = 0; i < n; ++i)
		{
			record[text.charCodeAt(i)] = record[text.charCodeAt(i)] + 1;
		}
		// Find second most occurring character
		for (var i = 0; i < n; ++i)
		{
			if (record[text.charCodeAt(i)] > record[text.charCodeAt(first)])
			{
				second = first;
				first = i;
			}
			else if (record[text.charCodeAt(i)] < record[text.charCodeAt(first)])
			{
				if (second == -1 || 
                    record[text.charCodeAt(second)] < record[text.charCodeAt(i)])
				{
					second = i;
				}
			}
		}
		process.stdout.write("\n Given Text : " + text);
		if (second == -1)
		{
			process.stdout.write("\n Second most frequent element not exist");
		}
		else
		{
			process.stdout.write("\n Second most frequent element is : " +
                                 text.charAt(second));
		}
	}
}

function main()
{
	var task = new Occurrence();
	// Given Sting text
	var text1 = "xyoxoxx";
	var text2 = "abaacdccec";
	var text3 = "xxyyzz";
	/*
	    Test A
	    ---------
	    text   = "xyoxoxx"
	    "xxxx" = 4
	    "y"    = 1
	    "oo"   = 2
	    -------------------------------
	    o = 2 which is second largest
	*/
	task.secondMostFrequent(text1);
	/*
	    Test B 
	    ---------
	    text   = "abaacdccec"
	    "aaa"  = 3
	    "b"    = 1
	    "cccc" = 4
	    "d"    = 1
	    -------------------------------
	    a = 3 which is second largest
	*/
	task.secondMostFrequent(text2);
	/*
	    Test C
	    ---------
	    "xx" = 2
	    "yy" = 2
	    "zz" = 2
	    -------------------------------
	    No second most frequent element
	*/
	task.secondMostFrequent(text3);
}
main();

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
#    Python 3 program for
#    Find the second most frequent character in a given string
class Occurrence :
	def secondMostFrequent(self, text) :
		n = len(text)
		if (n == 0) :
			return
		
		first = 0
		second = -1
		record = [0] * (256)
		i = 0
		#  Count frequency of character element
		while (i < n) :
			record[ord(text[i])] = record[ord(text[i])] + 1
			i += 1
		
		i = 0
		#  Find second most occurring character
		while (i < n) :
			if (record[ord(text[i])] > record[ord(text[first])]) :
				second = first
				first = i
			elif (record[ord(text[i])] < record[ord(text[first])]) :
				if (second == -1 or 
                    record[ord(text[second])] < record[ord(text[i])]) :
					second = i
				
			
			i += 1
		
		print("\n Given Text : ", text, end = "")
		if (second == -1) :
			print("\n Second most frequent element not exist", end = "")
		else :
			print("\n Second most frequent element is : ", text[second], end = "")
		
	

def main() :
	task = Occurrence()
	#  Given Sting text
	text1 = "xyoxoxx"
	text2 = "abaacdccec"
	text3 = "xxyyzz"
	#    Test A
	#    ---------
	#    text   = "xyoxoxx"
	#    "xxxx" = 4
	#    "y"    = 1
	#    "oo"   = 2
	#    -------------------------------
	#    o = 2 which is second largest
	task.secondMostFrequent(text1)
	#    Test B 
	#    ---------
	#    text   = "abaacdccec"
	#    "aaa"  = 3
	#    "b"    = 1
	#    "cccc" = 4
	#    "d"    = 1
	#    -------------------------------
	#    a = 3 which is second largest
	task.secondMostFrequent(text2)
	#    Test C
	#    ---------
	#    "xx" = 2
	#    "yy" = 2
	#    "zz" = 2
	#    -------------------------------
	#    No second most frequent element
	task.secondMostFrequent(text3)

if __name__ == "__main__": main()

Output

 Given Text :  xyoxoxx
 Second most frequent element is :  o
 Given Text :  abaacdccec
 Second most frequent element is :  a
 Given Text :  xxyyzz
 Second most frequent element not exist
#    Ruby program for
#    Find the second most frequent character in a given string
class Occurrence 
	def secondMostFrequent(text) 
		n = text.length
		if (n == 0) 
			return
		end

		first = 0
		second = -1
		record = Array.new(256) {0}
		i = 0
		#  Count frequency of character element
		while (i < n) 
			record[text[i].ord] = record[text[i].ord] + 1
			i += 1
		end

		i = 0
		#  Find second most occurring character
		while (i < n) 
			if (record[text[i].ord] > record[text[first].ord]) 
				second = first
				first = i
			elsif (record[text[i].ord] < record[text[first].ord]) 
				if (second == -1 || 
                    record[text[second].ord] < record[text[i].ord]) 
					second = i
				end

			end

			i += 1
		end

		print("\n Given Text : ", text)
		if (second == -1) 
			print("\n Second most frequent element not exist")
		else
 
			print("\n Second most frequent element is : ", text[second])
		end

	end

end

def main() 
	task = Occurrence.new()
	#  Given Sting text
	text1 = "xyoxoxx"
	text2 = "abaacdccec"
	text3 = "xxyyzz"
	#    Test A
	#    ---------
	#    text   = "xyoxoxx"
	#    "xxxx" = 4
	#    "y"    = 1
	#    "oo"   = 2
	#    -------------------------------
	#    o = 2 which is second largest
	task.secondMostFrequent(text1)
	#    Test B 
	#    ---------
	#    text   = "abaacdccec"
	#    "aaa"  = 3
	#    "b"    = 1
	#    "cccc" = 4
	#    "d"    = 1
	#    -------------------------------
	#    a = 3 which is second largest
	task.secondMostFrequent(text2)
	#    Test C
	#    ---------
	#    "xx" = 2
	#    "yy" = 2
	#    "zz" = 2
	#    -------------------------------
	#    No second most frequent element
	task.secondMostFrequent(text3)
end

main()

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
import scala.collection.mutable._;
/*
    Scala program for
    Find the second most frequent character in a given string
*/
class Occurrence()
{
	def secondMostFrequent(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		var first: Int = 0;
		var second: Int = -1;
		var record: Array[Int] = Array.fill[Int](256)(0);
		var i: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
			record(text.charAt(i)) = record(text.charAt(i)) + 1;
			i += 1;
		}
		i = 0;
		// Find second most occurring character
		while (i < n)
		{
			if (record(text.charAt(i)) > record(text.charAt(first)))
			{
				second = first;
				first = i;
			}
			else if (record(text.charAt(i)) < record(text.charAt(first)))
			{
				if (second == -1 || 
                    record(text.charAt(second)) < record(text.charAt(i)))
				{
					second = i;
				}
			}
			i += 1;
		}
		print("\n Given Text : " + text);
		if (second == -1)
		{
			print("\n Second most frequent element not exist");
		}
		else
		{
			print("\n Second most frequent element is : " + text.charAt(second));
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Occurrence = new Occurrence();
		// Given Sting text
		var text1: String = "xyoxoxx";
		var text2: String = "abaacdccec";
		var text3: String = "xxyyzz";
		/*
		    Test A
		    ---------
		    text   = "xyoxoxx"
		    "xxxx" = 4
		    "y"    = 1
		    "oo"   = 2
		    -------------------------------
		    o = 2 which is second largest
		*/
		task.secondMostFrequent(text1);
		/*
		    Test B 
		    ---------
		    text   = "abaacdccec"
		    "aaa"  = 3
		    "b"    = 1
		    "cccc" = 4
		    "d"    = 1
		    -------------------------------
		    a = 3 which is second largest
		*/
		task.secondMostFrequent(text2);
		/*
		    Test C
		    ---------
		    "xx" = 2
		    "yy" = 2
		    "zz" = 2
		    -------------------------------
		    No second most frequent element
		*/
		task.secondMostFrequent(text3);
	}
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist
import Foundation;
/*
    Swift 4 program for
    Find the second most frequent character in a given string
*/
class Occurrence
{
	func secondMostFrequent(_ data: String)
	{
		let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		var first: Int = 0;
		var second: Int = -1;
		var record: [Int] = Array(repeating: 0, count: 256);
		var i: Int = 0;
		var index: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
			index = Int(UnicodeScalar(String(text[i]))!.value);
			record[index] += 1;
			i += 1;
		}
		i = 0;
		// Find second most occurring character
		while (i < n)
		{
			index = Int(UnicodeScalar(String(text[i]))!.value);
			let f = Int(UnicodeScalar(String(text[first]))!.value);
	
			if (record[index] > record[f])
			{
				second = first;
				first = i;
			}
			else if (record[index] < record[f])
			{
				if (second == -1 ||
                    record[Int(UnicodeScalar(String(text[second]))!.value)] 
                    < record[index])
				{
					second = i;
				}
			}
			i += 1;
		}
		print("\n Given Text : ", data, terminator: "");
		if (second == -1)
		{
			print("\n Second most frequent element not exist", 
                  terminator: "");
		}
		else
		{
			print("\n Second most frequent element is : ", 
                  text[second], terminator: "");
		}
	}
}
func main()
{
	let task: Occurrence = Occurrence();
	// Given Sting text
	let text1: String = "xyoxoxx";
	let text2: String = "abaacdccec";
	let text3: String = "xxyyzz";
	/*
	    Test A
	    ---------
	    text   = "xyoxoxx"
	    "xxxx" = 4
	    "y"    = 1
	    "oo"   = 2
	    -------------------------------
	    o = 2 which is second largest
	*/
	task.secondMostFrequent(text1);
	/*
	    Test B 
	    ---------
	    text   = "abaacdccec"
	    "aaa"  = 3
	    "b"    = 1
	    "cccc" = 4
	    "d"    = 1
	    -------------------------------
	    a = 3 which is second largest
	*/
	task.secondMostFrequent(text2);
	/*
	    Test C
	    ---------
	    "xx" = 2
	    "yy" = 2
	    "zz" = 2
	    -------------------------------
	    No second most frequent element
	*/
	task.secondMostFrequent(text3);
}
main();

Output

 Given Text :  xyoxoxx
 Second most frequent element is :  o
 Given Text :  abaacdccec
 Second most frequent element is :  a
 Given Text :  xxyyzz
 Second most frequent element not exist
/*
    Kotlin program for
    Find the second most frequent character in a given string
*/
class Occurrence
{
	fun secondMostFrequent(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		var first: Int = 0;
		var second: Int = -1;
		val record: Array < Int > = Array(256)
		{
			0
		};
		var i: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
			record[text.get(i).toInt()] = record[text.get(i).toInt()] + 1;
			i += 1;
		}
		i = 0;
		// Find second most occurring character
		while (i < n)
		{
			if (record[text.get(i).toInt()] > record[text.get(first).toInt()])
			{
				second = first;
				first = i;
			}
			else if (record[text.get(i).toInt()] < 
                     record[text.get(first).toInt()])
			{
				if (second == -1 || 
                    record[text.get(second).toInt()] <
                    record[text.get(i).toInt()])
				{
					second = i;
				}
			}
			i += 1;
		}
		print("\n Given Text : " + text);
		if (second == -1)
		{
			print("\n Second most frequent element not exist");
		}
		else
		{
			print("\n Second most frequent element is : " + text.get(second));
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Occurrence = Occurrence();
	// Given Sting text
	val text1: String = "xyoxoxx";
	val text2: String = "abaacdccec";
	val text3: String = "xxyyzz";
	/*
	    Test A
	    ---------
	    text   = "xyoxoxx"
	    "xxxx" = 4
	    "y"    = 1
	    "oo"   = 2
	    -------------------------------
	    o = 2 which is second largest
	*/
	task.secondMostFrequent(text1);
	/*
	    Test B 
	    ---------
	    text   = "abaacdccec"
	    "aaa"  = 3
	    "b"    = 1
	    "cccc" = 4
	    "d"    = 1
	    -------------------------------
	    a = 3 which is second largest
	*/
	task.secondMostFrequent(text2);
	/*
	    Test C
	    ---------
	    "xx" = 2
	    "yy" = 2
	    "zz" = 2
	    -------------------------------
	    No second most frequent element
	*/
	task.secondMostFrequent(text3);
}

Output

 Given Text : xyoxoxx
 Second most frequent element is : o
 Given Text : abaacdccec
 Second most frequent element is : a
 Given Text : xxyyzz
 Second most frequent element not exist

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