Remove all frequencies less than k characters from string

Here given code implementation process.

/*
    Java Program for
    Remove all frequencies less than k characters from string
*/
public class RemoveCharacters
{
	public void removeCharLessThanK(String text, int k)
	{
		// Get the length
		int n = text.length();

		if (n == 0 || k <= 0)
		{
			return;
		}
		String result = "";
		// Use to count character frequency
		int []count = new int[256];

		for (int i = 0; i < 256; ++i)
		{
			// Set initial values
			count[i] = 0;
		}
		// Count character frequency
		for (int i = 0; i < n; ++i)
		{
			count[text.charAt(i)]++;
		}
		for (int i = 0; i < n; ++i)
		{
			if(count[text.charAt(i)] >= k)
			{
				// Include the resultant character
				result = result + text.charAt(i);
			}
		}
		// Display given text
		System.out.println(" Given text : " + text);
      	System.out.println(" Given k  : " + k);
      	// Display result
		System.out.println(" Result     : " + result);
	}
	public static void main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		// k = 3
		task.removeCharLessThanK("movecodeerror", 3);
		// k = 4
		task.removeCharLessThanK("demoipkeeyaddressdream",4);
	}
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters
{
	public: void removeCharLessThanK(string text, int k)
	{
		// Get the length
		int n = text.length();
		if (n == 0 || k <= 0)
		{
			return;
		}
		string result = "";
		// Use to count character frequency
		int count[256];
		for (int i = 0; i < 256; ++i)
		{
			// Set initial values
			count[i] = 0;
		}
		// Count character frequency
		for (int i = 0; i < n; ++i)
		{
			count[text[i]]++;
		}
		for (int i = 0; i < n; ++i)
		{
			if (count[text[i]] >= k)
			{
				// Include the resultant character
				result = result  +  (text[i]);
			}
		}
		// Display given text
		cout << " Given text : " << text << endl;
		cout << " Given k  : " << k << endl;
		// Display result
		cout << " Result     : " << result << endl;
	}
};
int main()
{
	RemoveCharacters *task = new RemoveCharacters();
	// Test
	// k = 3
	task->removeCharLessThanK("movecodeerror", 3);
	// k = 4
	task->removeCharLessThanK("demoipkeeyaddressdream", 4);
	return 0;
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
package main
import "fmt"
/*
    Go Program for
    Remove all frequencies less than k characters from string
*/
type RemoveCharacters struct {}
func getRemoveCharacters() * RemoveCharacters {
	var me *RemoveCharacters = &RemoveCharacters {}
	return me
}
func(this RemoveCharacters) removeCharLessThanK(
  text string, k int) {
	// Get the length
	var n int = len(text)
	if n == 0 || k <= 0 {
		return
	}
	var result string = ""
	// Use to count character frequency
	var count = make([] int, 256)
	for i := 0 ; i < 256 ; i++ {
		// Set initial values
		count[i] = 0
	}
	// Count character frequency
	for i := 0 ; i < n ; i++ {
		count[text[i]]++
	}
	for i := 0 ; i < n ; i++ {
		if count[text[i]] >= k {
			// Include the resultant character
			result = result + string(text[i])
		}
	}
	// Display given text
	fmt.Println(" Given text : ", text)
	fmt.Println(" Given k  : ", k)
	// Display result
	fmt.Println(" Result     : ", result)
}
func main() {
	var task * RemoveCharacters = getRemoveCharacters()
	// Test
	// k = 3
	task.removeCharLessThanK("movecodeerror", 3)
	// k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4)
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
// Include namespace system
using System;
/*
    Csharp Program for
    Remove all frequencies less than k characters from string
*/
public class RemoveCharacters
{
	public void removeCharLessThanK(String text, int k)
	{
		// Get the length
		int n = text.Length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		String result = "";
		// Use to count character frequency
		int[] count = new int[256];
		for (int i = 0; i < 256; ++i)
		{
			// Set initial values
			count[i] = 0;
		}
		// Count character frequency
		for (int i = 0; i < n; ++i)
		{
			count[text[i]]++;
		}
		for (int i = 0; i < n; ++i)
		{
			if (count[text[i]] >= k)
			{
				// Include the resultant character
				result = result + text[i];
			}
		}
		// Display given text
		Console.WriteLine(" Given text : " + text);
		Console.WriteLine(" Given k  : " + k);
		// Display result
		Console.WriteLine(" Result     : " + result);
	}
	public static void Main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		// k = 3
		task.removeCharLessThanK("movecodeerror", 3);
		// k = 4
		task.removeCharLessThanK("demoipkeeyaddressdream", 4);
	}
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
<?php
/*
    Php Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters
{
	public	function removeCharLessThanK($text, $k)
	{
		// Get the length
		$n = strlen($text);
		if ($n == 0 || $k <= 0)
		{
			return;
		}
		$result = "";
		// Use to count character frequency
		$count = array_fill(0, 256, 0);
		// Count character frequency
		for ($i = 0; $i < $n; ++$i)
		{
			$count[ord($text[$i])]++;
		}
		for ($i = 0; $i < $n; ++$i)
		{
			if ($count[ord($text[$i])] >= $k)
			{
				// Include the resultant character
				$result = $result.strval($text[$i]);
			}
		}
		// Display given text
		echo(" Given text : ".$text.
			"\n");
		echo(" Given k  : ".$k.
			"\n");
		// Display result
		echo(" Result     : ".$result.
			"\n");
	}
}

function main()
{
	$task = new RemoveCharacters();
	// Test
	// k = 3
	$task->removeCharLessThanK("movecodeerror", 3);
	// k = 4
	$task->removeCharLessThanK("demoipkeeyaddressdream", 4);
}
main();

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
/*
    Node JS Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters
{
	removeCharLessThanK(text, k)
	{
		// Get the length
		var n = text.length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result = "";
		// Use to count character frequency
		var count = Array(256).fill(0);
		// Count character frequency
		for (var i = 0; i < n; ++i)
		{
			count[text.charCodeAt(i)]++;
		}
		for (var i = 0; i < n; ++i)
		{
			if (count[text.charCodeAt(i)] >= k)
			{
				// Include the resultant character
				result = result + text.charAt(i);
			}
		}
		// Display given text
		console.log(" Given text : " + text);
		console.log(" Given k  : " + k);
		// Display result
		console.log(" Result     : " + result);
	}
}

function main()
{
	var task = new RemoveCharacters();
	// Test
	// k = 3
	task.removeCharLessThanK("movecodeerror", 3);
	// k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4);
}
main();

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
#    Python 3 Program for
#    Remove all frequencies less than k characters from string
class RemoveCharacters :
	def removeCharLessThanK(self, text, k) :
		#  Get the length
		n = len(text)
		if (n == 0 or k <= 0) :
			return
		
		result = ""
		#  Use to count character frequency
		count = [0] * (256)
		i = 0
		#  Count character frequency
		while (i < n) :
			count[ord(text[i])] += 1
			i += 1
		
		i = 0
		while (i < n) :
			if (count[ord(text[i])] >= k) :
				#  Include the resultant character
				result = result + str(text[i])
			
			i += 1
		
		#  Display given text
		print(" Given text : ", text)
		print(" Given k  : ", k)
		#  Display result
		print(" Result     : ", result)
	

def main() :
	task = RemoveCharacters()
	#  Test
	#  k = 3
	task.removeCharLessThanK("movecodeerror", 3)
	#  k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4)

if __name__ == "__main__": main()

Output

 Given text :  movecodeerror
 Given k  :  3
 Result     :  oeoeerror
 Given text :  demoipkeeyaddressdream
 Given k  :  4
 Result     :  deeeddede
#    Ruby Program for
#    Remove all frequencies less than k characters from string
class RemoveCharacters 
	def removeCharLessThanK(text, k) 
		#  Get the length
		n = text.length
		if (n == 0 || k <= 0) 
			return
		end

		result = ""
		#  Use to count character frequency
		count = Array.new(256) {0}
		i = 0
		#  Count character frequency
		while (i < n) 
			count[text[i].ord] += 1
			i += 1
		end

		i = 0
		while (i < n) 
			if (count[text[i].ord] >= k) 
				#  Include the resultant character
				result = result + text[i].to_s
			end

			i += 1
		end

		#  Display given text
		print(" Given text : ", text, "\n")
		print(" Given k  : ", k, "\n")
		#  Display result
		print(" Result     : ", result, "\n")
	end

end

def main() 
	task = RemoveCharacters.new()
	#  Test
	#  k = 3
	task.removeCharLessThanK("movecodeerror", 3)
	#  k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4)
end

main()

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
import scala.collection.mutable._;
/*
    Scala Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters()
{
	def removeCharLessThanK(text: String, k: Int): Unit = {
		// Get the length
		var n: Int = text.length();
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result: String = "";
		// Use to count character frequency
		var count: Array[Int] = Array.fill[Int](256)(0);
		var i: Int = 0;
		// Count character frequency
		while (i < n)
		{
			count(text.charAt(i)) += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (count(text.charAt(i)) >= k)
			{
				// Include the resultant character
				result = result + text.charAt(i).toString();
			}
			i += 1;
		}
		// Display given text
		println(" Given text : " + text);
		println(" Given k  : " + k);
		// Display result
		println(" Result     : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: RemoveCharacters = new RemoveCharacters();
		// Test
		// k = 3
		task.removeCharLessThanK("movecodeerror", 3);
		// k = 4
		task.removeCharLessThanK("demoipkeeyaddressdream", 4);
	}
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede
import Foundation;
/*
    Swift 4 Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters
{
	func removeCharLessThanK(_ data: String, _ k: Int)
	{
      	let text = Array(data);
		// Get the length
		let n: Int = text.count;
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result: String = "";
		// Use to count character frequency
		var count: [Int] = Array(repeating: 0, count: 256);
		var i: Int = 0;
		// Count character frequency
		while (i < n)
		{
			count[Int(UnicodeScalar(String(text[i]))!.value)] += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (count[Int(UnicodeScalar(String(text[i]))!.value)] >= k)
			{
				// Include the resultant character
				result = result + String(text[i]);
			}
			i += 1;
		}
		// Display given text
		print(" Given text : ", data);
		print(" Given k  : ", k);
		// Display result
		print(" Result     : ", result);
	}
}
func main()
{
	let task: RemoveCharacters = RemoveCharacters();
	// Test
	// k = 3
	task.removeCharLessThanK("movecodeerror", 3);
	// k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4);
}
main();

Output

 Given text :  movecodeerror
 Given k  :  3
 Result     :  oeoeerror
 Given text :  demoipkeeyaddressdream
 Given k  :  4
 Result     :  deeeddede
/*
    Kotlin Program for
    Remove all frequencies less than k characters from string
*/
class RemoveCharacters
{
	fun removeCharLessThanK(text: String, k: Int): Unit
	{
		// Get the length
		val n: Int = text.length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result: String = "";
		// Use to count character frequency
		val count: Array < Int > = Array(256)
		{
			0
		};
		var i: Int = 0;
		// Count character frequency
		while (i < n)
		{
			count[text.get(i).toInt()] += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (count[text.get(i).toInt()] >= k)
			{
				// Include the resultant character
				result = result + text.get(i).toString();
			}
			i += 1;
		}
		// Display given text
		println(" Given text : " + text);
		println(" Given k  : " + k);
		// Display result
		println(" Result     : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: RemoveCharacters = RemoveCharacters();
	// Test
	// k = 3
	task.removeCharLessThanK("movecodeerror", 3);
	// k = 4
	task.removeCharLessThanK("demoipkeeyaddressdream", 4);
}

Output

 Given text : movecodeerror
 Given k  : 3
 Result     : oeoeerror
 Given text : demoipkeeyaddressdream
 Given k  : 4
 Result     : deeeddede


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