Skip to main content

Find the frequency of characters in a string

Here given code implementation process.

// Java Program 
// Find the frequency of characters in a string
import java.util.HashMap;
public class Frequency
{
	public void countFrequency(String text)
	{
		// Use to collect character frequency
		HashMap < Character, Integer > record = new HashMap < > ();
		// iterating  loop through by text length
		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);
			}
		}
		// Display calculating character frequency
		for (char key: record.keySet())
		{
			System.out.print(key + " : " + record.get(key) + "\n");
		}
	}
	public static void main(String[] args)
	{
		Frequency task = new Frequency();
		// Given text
		String text = "WonderfulCodeAlgorithmoo7";
		// Test
		task.countFrequency(text);
	}
}

Output

A : 1
C : 1
d : 2
e : 2
f : 1
g : 1
h : 1
i : 1
l : 2
m : 1
n : 1
o : 5
r : 2
t : 1
u : 1
W : 1
7 : 1
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;
// C++ Program
// Find the frequency of characters in a string
class Frequency
{
	public: void countFrequency(string text)
	{
		// Use to collect character frequency
		unordered_map < char, int > record ;
		// iterating  loop through by text length
		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;
			}
		}

		for (auto& item : record)
		{
			cout << item.first << " : " <<  item.second << endl;
		}
       
	}
};
int main()
{
	Frequency task = Frequency();
	// Given text
	string text = "WonderfulCodeAlgorithmoo7";
	// Test
	task.countFrequency(text);
	return 0;
}

Output

m : 1
h : 1
t : 1
i : 1
g : 1
d : 2
A : 1
o : 5
C : 1
n : 1
W : 1
7 : 1
e : 2
r : 2
f : 1
u : 1
l : 2
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Find the frequency of characters in a string
public class Frequency
{
	public void countFrequency(String text)
	{
		// Use to collect character frequency
		Dictionary < char, int > record = new Dictionary < char, int > ();
		// iterating  loop through by text length
		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 > entry  in record)
		{
			Console.Write(entry.Key + " : " + entry.Value + "\n");
		}
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		// Given text
		String text = "WonderfulCodeAlgorithmoo7";
		// Test
		task.countFrequency(text);
	}
}

Output

W : 1
o : 5
n : 1
d : 2
e : 2
r : 2
f : 1
u : 1
l : 2
C : 1
A : 1
g : 1
i : 1
t : 1
h : 1
m : 1
7 : 1
<?php
// Php Program
// Find the frequency of characters in a string
class Frequency
{
	public	function countFrequency($text)
	{
		// Use to collect character frequency
		$record = array();
		// iterating  loop through by text length
		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 )
		{
			echo $key ." : ". $value ."\n";
		}
	}
}

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

Output

W : 1
o : 5
n : 1
d : 2
e : 2
r : 2
f : 1
u : 1
l : 2
C : 1
A : 1
g : 1
i : 1
t : 1
h : 1
m : 1
7 : 1
// Node Js Program
// Find the frequency of characters in a string
class Frequency
{
	countFrequency(text)
	{
		// Use to collect character frequency
		var record = new Map();
		// iterating  loop through by text length
		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)
		{
			process.stdout.write(key + " : " + value + "\n");
		}
	}
}

function main()
{
	var task = new Frequency();
	// Given text
	var text = "WonderfulCodeAlgorithmoo7";
	// Test
	task.countFrequency(text);
}
main();

Output

W : 1
o : 5
n : 1
d : 2
e : 2
r : 2
f : 1
u : 1
l : 2
C : 1
A : 1
g : 1
i : 1
t : 1
h : 1
m : 1
7 : 1
#  Python 3 Program
#  Find the frequency of characters in a string
class Frequency :
	def countFrequency(self, text) :
		#  Use to collect character frequency
		record = dict()
		#  iterating  loop through by text length
		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() :
			print(key ," : ", value )
		
	

def main() :
	task = Frequency()
	#  Given text
	text = "WonderfulCodeAlgorithmoo7"
	#  Test
	task.countFrequency(text)

if __name__ == "__main__": main()

Output

e  :  2
7  :  1
r  :  2
g  :  1
u  :  1
f  :  1
i  :  1
A  :  1
n  :  1
m  :  1
t  :  1
l  :  2
C  :  1
o  :  5
W  :  1
d  :  2
h  :  1
#  Ruby Program
#  Find the frequency of characters in a string
class Frequency 
	def countFrequency(text) 
		#  Use to collect character frequency
		record = Hash.new
		#  iterating  loop through by text length
		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 |  puts "  #{key}  :  #{value}" }
	end

end

def main() 
	task = Frequency.new()
	#  Given text
	text = "WonderfulCodeAlgorithmoo7"
	#  Test
	task.countFrequency(text)
end

main()

Output

  W  :  1
  o  :  5
  n  :  1
  d  :  2
  e  :  2
  r  :  2
  f  :  1
  u  :  1
  l  :  2
  C  :  1
  A  :  1
  g  :  1
  i  :  1
  t  :  1
  h  :  1
  m  :  1
  7  :  1
import scala.collection.mutable._;
// Scala Program
// Find the frequency of characters in a string
class Frequency
{
	def countFrequency(text: String): Unit = {
		// Use to collect character frequency
		var record: Map[Char, Int] = Map();
		// iterating  loop through by text length
		var i: Int = 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;
		}
        // Display calculating character frequency
        record.keys.foreach
        {
            key => println("  " + key + "  :  " + record(key))
        }
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		// Given text
		var text: String = "WonderfulCodeAlgorithmoo7";
		// Test
		task.countFrequency(text);
	}
}

Output

  A  :  1
  C  :  1
  d  :  2
  e  :  2
  f  :  1
  g  :  1
  h  :  1
  i  :  1
  l  :  2
  m  :  1
  n  :  1
  o  :  5
  r  :  2
  t  :  1
  u  :  1
  7  :  1
  W  :  1
import Foundation
// Swift 4 Program
// Find the frequency of characters in a string
class Frequency
{
	func countFrequency(_ t: String)
	{
		// Use to collect character frequency
		var text = Array(t);
		var record = [Character: Int]();
		// iterating  loop through by text length
		var i: Int = 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 (k, v) in record
		{
			print("  \(k)  :  \(v)")
		}
	}
}
func main()
{
	let task: Frequency = Frequency();
	// Given text
	let text: String = "WonderfulCodeAlgorithmoo7";
	// Test
	task.countFrequency(text);
}
main();

Output

  e  :  2
  h  :  1
  7  :  1
  C  :  1
  d  :  2
  W  :  1
  g  :  1
  m  :  1
  A  :  1
  u  :  1
  f  :  1
  r  :  2
  t  :  1
  o  :  5
  l  :  2
  n  :  1
  i  :  1
// Kotlin Program
// Find the frequency of characters in a string
class Frequency
{
	fun countFrequency(text: String): Unit
	{
		// Use to collect character frequency
		var record = mutableMapOf<Char, Int>();
		// iterating  loop through by text length
		var i: Int = 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;
		}
		// Display calculating character frequency
		for (key in record.keys)
		{
			print(key + " : " + record.getValue(key) + "\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Frequency = Frequency();
	// Given text
	var text: String = "WonderfulCodeAlgorithmoo7";
	// Test
	task.countFrequency(text);
}

Output

W : 1
o : 5
n : 1
d : 2
e : 2
r : 2
f : 1
u : 1
l : 2
C : 1
A : 1
g : 1
i : 1
t : 1
h : 1
m : 1
7 : 1
Frequency of characters in a string




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