Remove characters that appear more than k times

Here given code implementation process.

/*
    Java Program for
    Remove characters that appear more than k times
*/
public class RemoveCharacters
{
	public void removeMoreThanKchar(String text, int k)
	{
		// Get the length
		int n = text.length();
		if (n == 0 || k <= 0)
		{
			return;
		}
		String result = "";
		int[] count = new int[256];
		// Set initial frequency
		for (int i = 0; i < 256; ++i)
		{
			count[i] = 0;
		}
		// Count the 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)
			{
				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.removeMoreThanKchar("aatpaicountamount", 3);
		// k = 4
		task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
	}
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters
{
	public: void removeMoreThanKchar(string text, int k)
	{
		// Get the length
		int n = text.length();
		if (n == 0 || k <= 0)
		{
			return;
		}
		string result = "";
		int count[256];
		// Set initial frequency
		for (int i = 0; i < 256; ++i)
		{
			count[i] = 0;
		}
		// Count the frequency
		for (int i = 0; i < n; ++i)
		{
			count[text[i]]++;
		}
		for (int i = 0; i < n; ++i)
		{
			if (count[text[i]] < k)
			{
				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->removeMoreThanKchar("aatpaicountamount", 3);
	// k = 4
	task->removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
	return 0;
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
package main
import "fmt"
/*
    Go Program for
    Remove characters that appear more than k times
*/
type RemoveCharacters struct {}
func getRemoveCharacters() * RemoveCharacters {
	var me *RemoveCharacters = &RemoveCharacters {}
	return me
}
func(this RemoveCharacters) removeMoreThanKchar(
	text string, k int) {
	// Get the length
	var n int = len(text)
	if n == 0 || k <= 0 {
		return
	}
	var result string = ""
	var count = make([] int, 256)
	// Set initial frequency
	for i := 0 ; i < 256 ; i++ {
		count[i] = 0
	}
	// Count the frequency
	for i := 0 ; i < n ; i++ {
		count[int(text[i])]++
	}
	for i := 0 ; i < n ; i++ {
		if count[int(text[i])] < k {
			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.removeMoreThanKchar("aatpaicountamount", 3)
	// k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4)
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
// Include namespace system
using System;
/*
    Csharp Program for
    Remove characters that appear more than k times
*/
public class RemoveCharacters
{
	public void removeMoreThanKchar(String text, int k)
	{
		// Get the length
		int n = text.Length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		String result = "";
		int[] count = new int[256];
		// Set initial frequency
		for (int i = 0; i < 256; ++i)
		{
			count[i] = 0;
		}
		// Count the frequency
		for (int i = 0; i < n; ++i)
		{
			count[text[i]]++;
		}
		for (int i = 0; i < n; ++i)
		{
			if (count[text[i]] < k)
			{
				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.removeMoreThanKchar("aatpaicountamount", 3);
		// k = 4
		task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
	}
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
<?php
/*
    Php Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters
{
	public	function removeMoreThanKchar($text, $k)
	{
		// Get the length
		$n = strlen($text);
		if ($n == 0 || $k <= 0)
		{
			return;
		}
		$result = "";
		$count = array_fill(0, 256, 0);
		// Count the frequency
		for ($i = 0; $i < $n; ++$i)
		{
			$count[ord($text[$i])]++;
		}
		for ($i = 0; $i < $n; ++$i)
		{
			if ($count[ord($text[$i])] < $k)
			{
				// Include characters which appear less than K
				$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->removeMoreThanKchar("aatpaicountamount", 3);
	// k = 4
	$task->removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
}
main();

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
/*
    Node JS Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters
{
	removeMoreThanKchar(text, k)
	{
		// Get the length
		var n = text.length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result = "";
		var count = Array(256).fill(0);
		// Count the frequency
		for (var i = 0; i < n; ++i)
		{
			count[text.charAt(i).charCodeAt(0)]++;
		}
		for (var i = 0; i < n; ++i)
		{
			if (count[text.charAt(i).charCodeAt(0)] < k)
			{
				// Include characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3);
	// k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
}
main();

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
#    Python 3 Program for
#    Remove characters that appear more than k times
class RemoveCharacters :
	def removeMoreThanKchar(self, text, k) :
		#  Get the length
		n = len(text)
		if (n == 0 or k <= 0) :
			return
		
		result = ""
		count = [0] * (256)
		i = 0
		#  Count the frequency
		while (i < n) :
			count[ord(text[i])] += 1
			i += 1
		
		i = 0
		while (i < n) :
			if (count[ord(text[i])] < k) :
				#  Include characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3)
	#  k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4)

if __name__ == "__main__": main()

Output

 Given text :  aatpaicountamount
 Given k :  3
 Result     :  picounmoun
 Given text :  txy111iisooptcountinodiip11
 Given k :  4
 Result     :  txysptcuntndp
#    Ruby Program for
#    Remove characters that appear more than k times
class RemoveCharacters 
	def removeMoreThanKchar(text, k) 
		#  Get the length
		n = text.length
		if (n == 0 || k <= 0) 
			return
		end

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

		i = 0
		while (i < n) 
			if (count[text[i].ord] < k) 
				#  Include characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3)
	#  k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4)
end

main()

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
import scala.collection.mutable._;
/*
    Scala Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters()
{
	def removeMoreThanKchar(text: String, k: Int): Unit = {
		// Get the length
		var n: Int = text.length();
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result: String = "";
		var count: Array[Int] = Array.fill[Int](256)(0);
		var i: Int = 0;
		// Count the frequency
		while (i < n)
		{
			count(text.charAt(i)) += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (count(text.charAt(i)) < k)
			{
				// Include characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3);
		// k = 4
		task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
	}
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp
import Foundation;
/*
    Swift 4 Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters
{
	func removeMoreThanKchar(_ 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 = "";
		var count: [Int] = Array(repeating: 0, count: 256);
		var i: Int = 0;
		// Count the 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 characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3);
	// k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
}
main();

Output

 Given text :  aatpaicountamount
 Given k :  3
 Result     :  picounmoun
 Given text :  txy111iisooptcountinodiip11
 Given k :  4
 Result     :  txysptcuntndp
/*
    Kotlin Program for
    Remove characters that appear more than k times
*/
class RemoveCharacters
{
	fun removeMoreThanKchar(text: String, k: Int): Unit
	{
		// Get the length
		val n: Int = text.length;
		if (n == 0 || k <= 0)
		{
			return;
		}
		var result: String = "";
		val count: Array < Int > = Array(256)
		{
			0
		};
		var i: Int = 0;
		// Count the 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 characters which appear less than K
				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.removeMoreThanKchar("aatpaicountamount", 3);
	// k = 4
	task.removeMoreThanKchar("txy111iisooptcountinodiip11", 4);
}

Output

 Given text : aatpaicountamount
 Given k : 3
 Result     : picounmoun
 Given text : txy111iisooptcountinodiip11
 Given k : 4
 Result     : txysptcuntndp


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