Print the characters by frequency

Here given code implementation process.

// Java Program 
// Print the characters by frequency
import java.util.HashMap;
import java.util.ArrayList;
public class Frequency
{
	public void findFrequency(String text)
	{
		// 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
			{
				// Add new character record
				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 > ());
			}
			result.get(record.get(key)).add(key);
		}
		// Display element by Occurrences
		for (int freq: result.keySet())
		{
			ArrayList < Character > ans = result.get(freq);
			System.out.print(" Frequency : " + freq + "\n");
			// Display character
			for (char data: ans)
			{
				System.out.print("  " + data);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Frequency task = new Frequency();
      	// Given text
		String text = "abcaaafklZfdelebefa";
		task.findFrequency(text);
	}
}

Output

 Frequency : 1
  c  d  Z  k
 Frequency : 2
  b  l
 Frequency : 3
  e  f
 Frequency : 5
  a
// Include header file
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
using namespace std;

// C++ Program
// Print the characters by frequency

class Frequency
{
	public: void findFrequency(string text)
	{
		// Use to collect character frequency
		unordered_map < char, int > record ;
      	// Use to combine elements by frequency
		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
			{
				// Add new character record
				record[text[i]] = 1;
			}
		}
      	// Group characters by frequency
		for (auto &key: record)
		{
			result[key.second].push_back(key.first);
		}
      	// Display calculated result
		for (auto &freq: result)
		{
			vector < char > ans = result[freq.first];
			cout << " Frequency : " << freq.first << "\n";
			for (vector<char>::iterator data = ans.begin(); 
                 data != ans.end(); data++)
			{
				cout << "  " << *data;
			}
			cout << "\n";
		}
	}
};
int main()
{
	Frequency task = Frequency();
	// Given text
	string text = "abcaaafklZfdelebefa";
	task.findFrequency(text);
	return 0;
}

Output

 Frequency : 1
  d  Z  k  c
 Frequency : 2
  l  b
 Frequency : 3
  e  f
 Frequency : 5
  a
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Print the characters by frequency
public class Frequency
{
	public void findFrequency(String text)
	{
		// Use to collect character frequency
		Dictionary < char, int > record = new Dictionary < char, int > ();
		Dictionary < int, List < char > > result = new Dictionary < 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
			{
				// Add new character record
				record.Add(text[i], 1);
			}
		}
		foreach(KeyValuePair < char, int > info in record)
		{
			if (result.ContainsKey(info.Value) == false)
			{
				result.Add(record[info.Key], new List < char > ());
			}
			result[info.Value].Add(info.Key);
		}
		foreach(KeyValuePair < int, List < char > > freq in result)
		{
			List < char > ans = freq.Value;
			Console.Write(" Frequency : " + freq.Key + "\n");
			foreach(char data in ans)
			{
				Console.Write("  " + data);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		// Given text
		String text = "abcaaafklZfdelebefa";
		task.findFrequency(text);
	}
}

Output

 Frequency : 5
  a
 Frequency : 2
  b  l
 Frequency : 1
  c  k  Z  d
 Frequency : 3
  f  e
<?php
// Php Program
// Print the characters by frequency
class Frequency
{
	public	function findFrequency($text)
	{
		// 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)
		{
			$result[$record[$key]][] = $key;
		}
		foreach($result as $freq => $value)
		{
			$ans =  $value;
			echo " Frequency : ". $freq ."\n";
			foreach($ans as $data)
			{
				echo "  ". $data;
			}
			echo "\n";
		}
	}
}

function main()
{
	$task = new Frequency();
	// Given text
	$text = "abcaaafklZfdelebefa";
	$task->findFrequency($text);
}
main();

Output

 Frequency : 5
  a
 Frequency : 2
  b  l
 Frequency : 1
  c  k  Z  d
 Frequency : 3
  f  e
// Node Js Program
// Print the characters by frequency
class Frequency
{
	findFrequency(text)
	{
		// 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
			{
				// Add new character record
				record.set(text.charAt(i), 1);
			}
		}
		for (let [key, value] of record)
		{
			if (result.has(value) == false)
			{
				result.set(record.get(key), []);
			}
			result.get(value).push(key);
		}
		for (let [freq, value] of result)
		{
			var ans = value;
			console.log(" Frequency : " + freq );
			for (let data of ans)
			{
				process.stdout.write("  " + data);
			}
			console.log();
		}
	}
}

function main()
{
	var task = new Frequency();
	// Given text
	var text = "abcaaafklZfdelebefa";
	task.findFrequency(text);
}
main();

Output

 Frequency : 5
  a
 Frequency : 2
  b  l
 Frequency : 1
  c  k  Z  d
 Frequency : 3
  f  e
#  Python 3 Program 
#  Print the characters by frequency
class Frequency :
    def findFrequency(self, text) :
        #  Use to collect character frequency
        record = dict() 
        result = dict() 
        #  Find the frequency of character
        i = 0
        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 :
                #  Add new character record
                record[text[i]] = 1
            i += 1

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

        for freq, value in result.items() :
            ans = result.get(freq)
            print(" Frequency : ", freq )
            for data in ans :
                print("  ", data, end = "")
            print(end = "\n")

def main() :
    task = Frequency()
    #  Given text
    text = "abcaaafklZfdelebefa"
    task.findFrequency(text)

if __name__ == "__main__": main()

Output

 Frequency :  1
   c   d   k   Z
 Frequency :  2
   l   b
 Frequency :  3
   e   f
 Frequency :  5
   a
#  Ruby Program 
#  Print the characters by frequency
class Frequency 
	def findFrequency(text) 
		#  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)
        }
        result.each { | freq, value | 
            ans = value
            print(" Frequency : ", freq ,"\n")
            for data in ans do 
                print("  ", data)
            end
            print("\n")
        }
    end
end
def main() 
	task = Frequency.new()
	#  Given text
	text = "abcaaafklZfdelebefa"
	task.findFrequency(text)
end

main()

Output

 Frequency : 5
  a
 Frequency : 2
  b  l
 Frequency : 1
  c  k  Z  d
 Frequency : 3
  f  e
import scala.collection.mutable._;
// Scala Program
// Print the characters by frequency
class Frequency
{
	def findFrequency(text: String): 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
				record.addOne(text.charAt(i), record.get(text.charAt(i)).get + 1);
			}
			else
			{
				// Add new character record
				record.addOne(text.charAt(i), 1);
			}
			i += 1;
		}
		for ((k, v) <- record)
		{
			if (result.contains(v) == false)
			{
				result.addOne(v, new ArrayBuffer[Char]());
			}
			result.get(v).get.append(k);
		}
		for ((k, v) <- result)
		{
			var ans = v;
			print(" Frequency : " + k + "\n");
			for (data <- ans)
			{
				print("  " + data);
			}
			print("\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		// Given text
		var text: String = "abcaaafklZfdelebefa";
		task.findFrequency(text);
	}
}

Output

 Frequency : 1
  c  d  Z  k
 Frequency : 2
  b  l
 Frequency : 3
  e  f
 Frequency : 5
  a
import Foundation
// Swift 4 Program
// Print the characters by frequency
class Frequency
{
	func findFrequency(_ t: String)
	{
		// 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
			{
				// Add new character record
				record[text[i]] = 1;
			}
			i += 1;
		}
		for (key, value) in record
		{
			if (result.keys.contains(value) == false)
			{
				result[value] = [Character]();
			}
			result[value]!.append(key);
		}
		for (key, value) in result
		{
			let ans = value;
			print(" Frequency : ", key);
			for data in ans
			{
				print("  ", data, terminator: "");
			}
			print(terminator: "\n");
		}
	}
}
func main()
{
	let task: Frequency = Frequency();
	// Given text
	let text: String = "abcaaafklZfdelebefa";
	task.findFrequency(text);
}
main();

Output

 Frequency :  5
   a
 Frequency :  2
   b   l
 Frequency :  3
   f   e
 Frequency :  1
   c   d   Z   k
// Kotlin Program
// Print the characters by frequency
class Frequency
{
	fun findFrequency(text: String): 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
			{
				// Add new character record
				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> ());
			}
			result.getValue(record.getValue(key)).add(key);
		}
		// Display element by Occurrences
		for (key in result.keys)
		{
			
			var ans = result.getValue(key);
			print(" Frequency : " + key + "\n");
			// Display character
			for (data in ans)
			{
				print("  " + data);
			}
			print("\n");
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Frequency = Frequency();
	// Given text
	var text: String = "abcaaafklZfdelebefa";
	task.findFrequency(text);
}

Output

 Frequency : 5
  a
 Frequency : 2
  b  l
 Frequency : 1
  c  k  Z  d
 Frequency : 3
  f  e
Group elements by frequency


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







© 2021, kalkicode.com, All rights reserved