# Find the character having kth largest frequency

Here given code implementation process.

``````// Java Program
// Find the character having kth largest frequency
import java.util.HashMap;
import java.util.ArrayList;
public class Frequency
{
public void kthFrequency(String text, int k)
{
if (k < 1 || text.length() < k)
{
// Invalid inputs
return;
}
// Use to collect character frequency
HashMap < Character, Integer > record =
new HashMap < Character, Integer > ();
HashMap < Integer, ArrayList < Character > > result =
new HashMap < Integer, ArrayList < Character > > ();
// Find the frequency of character
for (int i = 0; i < text.length(); ++i)
{
if (record.containsKey(text.charAt(i)))
{
// When element exist
// Increase the element frequency
record.put(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.put(text.charAt(i), 1);
}
}
// Combine elements by frequency
for (char key: record.keySet())
{
if (result.containsKey(record.get(key)) == false)
{
result.put(record.get(key), new ArrayList < Character > ());
}
}
System.out.print("\n " + k + "-th Largest Frequency Element is \n");
if (result.size() < k)
{
System.out.print(" None \n");
}
else
{
// Get the key
int[] keyRecord = new int[result.size()];
int count = 0;
// Get key in array
for (int key: result.keySet())
{
keyRecord[count] = key;
count++;
}
// Get resultant node
ArrayList <Character> ans = result.get(keyRecord[keyRecord.length - k]);
System.out.print(" [");
// Display calculate character
for (char data: ans)
{
System.out.print("  " + data);
}
System.out.print(" ]");
}
}
public static void main(String[] args)
{
// Given text
String text = "77kkaacccbfkbb7aacfffddf7AAA";
}
}``````

#### Output

`````` 2-th Largest Frequency Element is
[  a  c  7 ]``````
``````// Include header file
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>

using namespace std;
// C++ Program
// Find the character having kth largest frequency
class Frequency
{
public: void kthFrequency(string text, int k)
{
// Invalid inputs
if (k < 1 || text.length() < k)
{
return;
}
// Use to collect character frequency
unordered_map < char, int > record ;
map < int, vector < char > > result ;
// Find the frequency of character
for (int i = 0; i < text.length(); ++i)
{
if (record.find(text[i]) != record.end())
{
// When element exist
// Increase the element frequency
record[text[i]] = record[text[i]] + 1;
}
else
{
record[text[i]] = 1;
}
}
for (auto &info: record)
{
result[info.second].push_back(info.first);
}
cout << "\n " << k << "-th Largest Frequency Element is \n";
if (result.size() < k)
{
cout << " None \n";
}
else
{
// Get the key
int *keyRecord = new int[result.size()];
int count = 0;
for (auto &info: result)
{
keyRecord[count] = info.first;
count++;
}
// Get resultant node
vector < char > ans = result[keyRecord[result.size() - k]];
cout << " [";
for (vector<char>::iterator data = ans.begin(); data != ans.end(); data++)
{
cout << "  " << *data;
}
cout << " ]";
}
}
};
int main()
{
// Given text
string text = "77kkaacccbfkbb7aacfffddf7AAA";
return 0;
}``````

#### Output

`````` 2-th Largest Frequency Element is
[  a  7  c ]``````
``````// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Find the character having kth largest frequency
public class Frequency
{
public void kthFrequency(String text, int k)
{
// Invalid inputs
if (k < 1 || text.Length < k)
{
return;
}
// Use to collect character frequency
SortedDictionary < char, int > record =
new SortedDictionary < char, int > ();
SortedDictionary < int, List < char > > result =
new SortedDictionary < int, List < char > > ();
// Find the frequency of character
for (int i = 0; i < text.Length; ++i)
{
if (record.ContainsKey(text[i]))
{
// When element exist
// Increase the element frequency
record[text[i]] = record[text[i]] + 1;
}
else
{
}
}
foreach(KeyValuePair < char, int > info in record)
{
if (result.ContainsKey(info.Value) == false)
{
result.Add(info.Value, new List < char > ());
}
}
Console.Write("\n " + k + "-th Largest Frequency Element is \n");
if (result.Count < k)
{
Console.Write(" None \n");
}
else
{
// Get the key
int[] keyRecord = new int[result.Count];
int count = 0;
foreach(KeyValuePair < int, List < char > > info in result)
{
keyRecord[count] = info.Key;
count++;
}
Array.Sort(keyRecord);
// Get resultant node
List < char > ans = result[keyRecord[result.Count - k]];
Console.Write(" [");
foreach(char data in ans)
{
Console.Write("  " + data);
}
Console.Write(" ]");
}
}
public static void Main(String[] args)
{
// Given text
String text = "77kkaacccbfkbb7aacfffddf7AAA";
}
}``````

#### Output

`````` 2-th Largest Frequency Element is
[  7  a  c ]``````
``````<?php
// Php Program
// Find the character having kth largest frequency
class Frequency
{
public  function kthFrequency(\$text, \$k)
{
// Invalid inputs
if (\$k < 1 || strlen(\$text) < \$k)
{
return;
}
// Use to collect character frequency
\$record = array();
\$result = array();
// Find the frequency of character
for (\$i = 0; \$i < strlen(\$text); ++\$i)
{
if (array_key_exists(\$text[\$i], \$record))
{ // When element exist
// Increase the element frequency
\$record[\$text[\$i]] = \$record[\$text[\$i]] + 1;
}
else
{ // Add new character record
\$record[\$text[\$i]] = 1;
}
}
foreach(\$record as \$key => \$value)
{
if (array_key_exists(\$value, \$result) == false)
{
\$result[\$value] = array();
}
\$result[\$value][] = \$key;
}
echo "\n ". \$k ."-th Largest Frequency Element is \n";
if (count(\$result) < \$k)
{
echo " None \n";
}
else
{
// Sort key
ksort(\$result);
// Get the key
\$keyRecord = array_fill(0, count(\$result), 0);
\$count = 0;
foreach(\$result as \$key => \$value)
{
\$keyRecord[\$count] = \$key;
\$count++;
}
// Get resultant node
\$ans = \$result[\$keyRecord[count(\$keyRecord) - \$k]];
echo " [";
foreach(\$ans as \$data)
{
echo "  ". \$data;
}
echo " ]";
}
}
}

function main()
{
// Given text
\$text = "77kkaacccbfkbb7aacfffddf7AAA";
}
main();``````

#### Output

`````` 2-th Largest Frequency Element is
[  7  a  c ]``````
``````// Node Js Program
// Find the character having kth largest frequency
class Frequency
{
kthFrequency(text, k)
{
// Invalid inputs
if (k < 1 || text.length < k)
{
return;
}
// Use to collect character frequency
var record = new Map();
var result = new Map();
// Find the frequency of character
for (var i = 0; i < text.length; ++i)
{
if (record.has(text.charAt(i)))
{
// When element exist
// Increase the element frequency
record.set(text.charAt(i), record.get(text.charAt(i)) + 1);
}
else
{
record.set(text.charAt(i), 1);
}
}
for (let [key, value] of record)
{
if (result.has(record.get(key)) == false)
{
result.set(record.get(key), []);
}
result.get(record.get(key)).push(key);
}
process.stdout.write("\n " + k + "-th Largest Frequency Element is \n");
if (result.size < k)
{
process.stdout.write(" None \n");
}
else
{
// Sort map by key
result = new Map([...result.entries()].sort());
// Get the key
var keyRecord = Array(result.size).fill(0);
var count = 0;
for (let [key, value] of result)
{
keyRecord[count] = key;
count++;
}
// Get resultant node
var ans = result.get(keyRecord[keyRecord.length - k]);
process.stdout.write(" [");
for (let data of ans)
{
process.stdout.write("  " + data);
}
process.stdout.write(" ]");
}
}
}

function main()
{
// Given text
var text = "77kkaacccbfkbb7aacfffddf7AAA";
}
main();``````

#### Output

`````` 2-th Largest Frequency Element is
[  7  a  c ]``````
``````#  Python 3 Program
#  Find the character having kth largest frequency
class Frequency :
def kthFrequency(self, text, k) :
if (k < 1 or len(text) < k) :
#  Invalid inputs
return

#  Use to collect character frequency
record = dict()
result = dict()
i = 0
#  Find the frequency of character
while (i < len(text)) :
if (text[i] in record.keys()) :
#  When element exist
#  Increase the element frequency
record[text[i]] = record.get(text[i]) + 1
else :
record[text[i]] = 1

i += 1

for key, value in record.items() :
if ((value in result.keys()) == False) :
result[value] = []
result.get(value).append(key)

print("\n ", k ,"-th Largest Frequency Element is ")
if (len(result) < k) :
print(" None ")
else :
#  Get the key
keyRecord = [0] * (len(result))
count = 0
for key, value in result.items() :
keyRecord[count] = key
count += 1
keyRecord.sort()
#  Get resultant node
ans = result.get(keyRecord[len(keyRecord) - k])
print(" [", end = "")
for data in ans :
print("  ", data, end = "")

print(" ]", end = "")

def main() :
#  Given text
text = "77kkaacccbfkbb7aacfffddf7AAA"

if __name__ == "__main__": main()``````

#### Output

``````  2 -th Largest Frequency Element is
[   7   c   a ]``````
``````#  Ruby Program
#  Find the character having kth largest frequency
class Frequency
def kthFrequency(text, k)
if (k < 1 || text.length < k)
#  Invalid inputs
return
end

#  Use to collect character frequency
record = Hash.new
result = Hash.new
#  Find the frequency of character
i = 0
while (i < text.length)
if (record.key?(text[i]))
record[text[i]] = record[text[i]] + 1
else
record[text[i]] = 1
end
i += 1
end

record.each { | key, value |
if (result.key?(value) == false)
result[value] = []
end

result[value].push(key)
}
print("\n ", k ,"-th Largest Frequency Element is \n")
if (result.size() < k)
print(" None \n")
else
# sort hash by key
result = result.sort.to_h
#  Get the key
keyRecord = Array.new(result.size()) {0}
count = 0
result.each { | key, value |
keyRecord[count] = key
count += 1
}

#  Get resultant node
ans = result[keyRecord[keyRecord.length - k]]
print(" [")

for data in ans do
print("  ", data)
end
print(" ]")
end

end

end

def main()
#  Given text
text = "77kkaacccbfkbb7aacfffddf7AAA"
end

main()``````

#### Output

`````` 2-th Largest Frequency Element is
[  7  a  c ]``````
``````import scala.collection.mutable._;
// Scala Program
// Find the character having kth largest frequency
class Frequency
{
def kthFrequency(text: String, k: Int): Unit = {
// Use to collect character frequency
var record = Map[Char, Int]();
var result = Map[Int, ArrayBuffer[Char]]();
// Find the frequency of character
var i = 0;
while (i < text.length())
{
if (record.contains(text.charAt(i)))
{
// When element exist
// Increase the element frequency
}
else
{
}
i += 1;
}
for ((k, v) <- record)
{
if (result.contains(v) == false)
{
}
result.get(v).get.append(k);
}
print("\n " + k + "-th Largest Frequency Element is \n");
if (result.size < k)
{
print(" None \n");
}
else
{
// Get the key
var keyRecord: Array[Int] = Array.fill[Int](result.size)(0);
var count: Int = 0;
for ((k, v) <- result)
{
keyRecord(count) = k;
count += 1;
}
// Get resultant node
var ans = result.get(keyRecord(keyRecord.length - k)).get;
print(" Frequency : " + k + "\n");
print(" [");
for (data <- ans)
{
print("  " + data);
}
print(" ]");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Frequency = new Frequency();
// Given text
var text: String = "77kkaacccbfkbb7aacfffddf7AAA";
}
}``````

#### Output

`````` 2-th Largest Frequency Element is
Frequency : 2
[  a  c  7 ]``````
``````import Foundation
// Swift 4 Program
// Find the character having kth largest frequency
class Frequency
{
func kthFrequency(_ t: String, _ k: Int)
{
// Use to collect character frequency
let text = Array(t);
var record = [Character: Int]();
var result = [Int: [Character]]();
// Find the frequency of character
var i = 0;
while (i < text.count)
{
if (record.keys.contains(text[i]))
{
// When element exist
// Increase the element frequency
record[text[i]] = record[text[i]]! + 1;
}
else
{
record[text[i]] = 1;
}
i += 1;
}
for (key, value) in record
{
if (result.keys.contains(value) == false)
{
result[value] = [Character]();
}
result[value]!.append(key);
}
print("\n ", k ,"-th Largest Frequency Element is ");
if (result.count < k)
{
print(" None ");
}
else
{
let temp = result.sorted { \$0.0 < \$1.0 };
// Get the key
var keyRecord: [Int] = Array(repeating: 0, count: result.count);
var count: Int = 0;
for (key, _) in temp
{
keyRecord[count] = key;
count += 1;
}
// Get resultant node

let ans = result[keyRecord[keyRecord.count - k]]!;
print(" [", terminator: "");
for data in ans
{
print("  ", data, terminator: "");
}
print(" ]", terminator: "");
}
}
}
func main()
{
// Given text
let text: String = "77kkaacccbfkbb7aacfffddf7AAA";
}
main();``````

#### Output

``````  2 -th Largest Frequency Element is
[   7   c   a ]``````
``````// Kotlin Program
// Find the character having kth largest frequency
class Frequency
{
fun kthFrequency(text: String, k: Int): Unit
{
// Use to collect character frequency
var record = mutableMapOf<Char, Int>();
var result = mutableMapOf < Int , ArrayList < Char > > ();
// Find the frequency of character
var i = 0;
while (i < text.length)
{
if (record.containsKey(text.get(i)))
{
// When element exist
// Increase the element frequency
record.put(text.get(i), record.getValue(text.get(i)) + 1);
}
else
{
record.put(text.get(i), 1);
}
i += 1;
}
// Combine elements by frequency
for (key in record.keys)
{
if (result.containsKey(record.getValue(key)) == false)
{
result.put(record.getValue(key), ArrayList <Char> ());
}
}
print("\n " + k + "-th Largest Frequency Element is \n");
if (result.count() < k)
{
print(" None \n");
}
else
{

// Get the key
var keyRecord: Array < Int > = Array(result.count())
{
0
};

var count: Int = 0;
// Get key in array
for (key in result.keys)
{
keyRecord[count] = key;
count += 1;
}
keyRecord.sort();
// Get resultant node
var ans = result.getValue(keyRecord[keyRecord.count() - k]);
print(" [");
// Display calculate character
for (data in ans)
{
print("  " + data);
}
print(" ]");
}
}
}
fun main(args: Array < String > ): Unit
{
// Given text
var text: String = "77kkaacccbfkbb7aacfffddf7AAA";
}``````

#### Output

`````` 2-th Largest Frequency Element is
[  7  a  c ]``````

## 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.