Print duplicate characters from string

Here given code implementation process.

// Java program for
// Print duplicate characters from string
public class Repetitive
{
	public void showDuplicate(String text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		int[] frequence = new int[256];
		boolean result = false;
		for (int i = 0; i < 256; ++i)
		{
			frequence[i] = 0;
		}
		// Count frequence of given text
		for (int i = 0; i < n; ++i)
		{
			frequence[text.charAt(i)]++;
		}
		System.out.println("\n Given Text : " + text);
		// Print duplicate characters 
		for (int i = 0; i < 256; ++i)
		{
			if (frequence[i] > 1)
			{
				result = true;
				System.out.print("  " + (char)(i));
			}
		}
		if (result == false)
		{
			System.out.println(" No result ");
		}
	}
	public static void main(String[] args)
	{
		Repetitive task = new Repetitive();
		// Test
		task.showDuplicate("complement");
		task.showDuplicate("decoration");
		task.showDuplicate("googlecode");
		task.showDuplicate("abc");
	}
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Print duplicate characters from string
class Repetitive
{
	public: void showDuplicate(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		int frequence[256];
		bool result = false;
		for (int i = 0; i < 256; ++i)
		{
			frequence[i] = 0;
		}
		// Count frequence of given text
		for (int i = 0; i < n; ++i)
		{
			frequence[text[i]]++;
		}
		cout << "\n Given Text : " << text << endl;
		// Print duplicate characters 
		for (int i = 0; i < 256; ++i)
		{
			if (frequence[i] > 1)
			{
				result = true;
				cout << "  " << (char)(i);
			}
		}
		if (result == false)
		{
			cout << " No result " << endl;
		}
	}
};
int main()
{
	Repetitive *task = new Repetitive();
	// Test
	task->showDuplicate("complement");
	task->showDuplicate("decoration");
	task->showDuplicate("googlecode");
	task->showDuplicate("abc");
	return 0;
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
// Include namespace system
using System;
// Csharp program for
// Print duplicate characters from string
public class Repetitive
{
	public void showDuplicate(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		int[] frequence = new int[256];
		Boolean result = false;
		for (int i = 0; i < 256; ++i)
		{
			frequence[i] = 0;
		}
		// Count frequence of given text
		for (int i = 0; i < n; ++i)
		{
			frequence[text[i]]++;
		}
		Console.WriteLine("\n Given Text : " + text);
		// Print duplicate characters 
		for (int i = 0; i < 256; ++i)
		{
			if (frequence[i] > 1)
			{
				result = true;
				Console.Write("  " + (char)(i));
			}
		}
		if (result == false)
		{
			Console.WriteLine(" No result ");
		}
	}
	public static void Main(String[] args)
	{
		Repetitive task = new Repetitive();
		// Test
		task.showDuplicate("complement");
		task.showDuplicate("decoration");
		task.showDuplicate("googlecode");
		task.showDuplicate("abc");
	}
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
package main
import "fmt"
// Go program for
// Print duplicate characters from string

func showDuplicate(text string) {
	var n int = len(text)
	if n == 0 {
		return
	}
	// Use to count frequency of possible characters
	var frequence = make([] int, 256)
	var result bool = false
	// Count frequence of given text
	for i := 0 ; i < n ; i++ {
		frequence[text[i]]++
	}
	fmt.Println("\n Given Text : ", text)
	// Print duplicate characters 
	for i := 0 ; i < 256 ; i++ {
		if frequence[i] > 1 {
			result = true
			fmt.Print("  ", string(i))
		}
	}
	if result == false {
		fmt.Println(" No result ")
	}
}
func main() {
	
	// Test
	showDuplicate("complement")
	showDuplicate("decoration")
	showDuplicate("googlecode")
	showDuplicate("abc")
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
<?php
// Php program for
// Print duplicate characters from string
class Repetitive
{
	public	function showDuplicate($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		$frequence = array_fill(0, 256, 0);
		$result = false;
		// Count frequence of given text
		for ($i = 0; $i < $n; ++$i)
		{
			$frequence[ord($text[$i])]++;
		}
		echo("\n Given Text : ".$text.
			"\n");
		// Print duplicate characters 
		for ($i = 0; $i < 256; ++$i)
		{
			if ($frequence[$i] > 1)
			{
				$result = true;
				echo("  ".chr(($i)));
			}
		}
		if ($result == false)
		{
			echo(" No result ".
				"\n");
		}
	}
}

function main()
{
	$task = new Repetitive();
	// Test
	$task->showDuplicate("complement");
	$task->showDuplicate("decoration");
	$task->showDuplicate("googlecode");
	$task->showDuplicate("abc");
}
main();

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
// Node JS program for
// Print duplicate characters from string
class Repetitive
{
	showDuplicate(text)
	{
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		var frequence = Array(256).fill(0);
		var result = false;
		// Count frequence of given text
		for (var i = 0; i < n; ++i)
		{
			frequence[text.charAt(i).charCodeAt(0)]++;
		}
		console.log("\n Given Text : " + text);
		// Print duplicate characters 
		for (var i = 0; i < 256; ++i)
		{
			if (frequence[i] > 1)
			{
				result = true;
				process.stdout.write("  " + String.fromCharCode((i)));
			}
		}
		if (result == false)
		{
			console.log(" No result ");
		}
	}
}

function main()
{
	var task = new Repetitive();
	// Test
	task.showDuplicate("complement");
	task.showDuplicate("decoration");
	task.showDuplicate("googlecode");
	task.showDuplicate("abc");
}
main();

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
#  Python 3 program for
#  Print duplicate characters from string
class Repetitive :
	def showDuplicate(self, text) :
		n = len(text)
		if (n == 0) :
			return
		
		#  Use to count frequency of possible characters
		frequence = [0] * (256)
		result = False
		i = 0
		#  Count frequence of given text
		while (i < n) :
			frequence[ord(text[i])] += 1
			i += 1
		
		print("\n Given Text : ", text)
		i = 0
		#  Print duplicate characters 
		while (i < 256) :
			if (frequence[i] > 1) :
				result = True
				print("  ", chr((i)), end = "")
			
			i += 1
		
		if (result == False) :
			print(" No result ")
		
	

def main() :
	task = Repetitive()
	#  Test
	task.showDuplicate("complement")
	task.showDuplicate("decoration")
	task.showDuplicate("googlecode")
	task.showDuplicate("abc")

if __name__ == "__main__": main()

Output

 Given Text :  complement
   e   m
 Given Text :  decoration
   o
 Given Text :  googlecode
   e   g   o
 Given Text :  abc
 No result
#  Ruby program for
#  Print duplicate characters from string
class Repetitive 
	def showDuplicate(text) 
		n = text.length
		if (n == 0) 
			return
		end

		#  Use to count frequency of possible characters
		frequence = Array.new(256) {0}
		result = false
		i = 0
		#  Count frequence of given text
		while (i < n) 
			frequence[text[i].ord] += 1
			i += 1
		end

		print("\n Given Text : ", text, "\n")
		i = 0
		#  Print duplicate characters 
		while (i < 256) 
			if (frequence[i] > 1) 
				result = true
				print("  ", ((i)).chr)
			end

			i += 1
		end

		if (result == false) 
			print(" No result ", "\n")
		end

	end

end

def main() 
	task = Repetitive.new()
	#  Test
	task.showDuplicate("complement")
	task.showDuplicate("decoration")
	task.showDuplicate("googlecode")
	task.showDuplicate("abc")
end

main()

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result 
import scala.collection.mutable._;
// Scala program for
// Print duplicate characters from string
class Repetitive()
{
	def showDuplicate(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		var frequence: Array[Int] = Array.fill[Int](256)(0);
		var result: Boolean = false;
		var i: Int = 0;
		// Count frequence of given text
		while (i < n)
		{
			frequence(text.charAt(i)) += 1;
			i += 1;
		}
		println("\n Given Text : " + text);
		i = 0;
		// Print duplicate characters 
		while (i < 256)
		{
			if (frequence(i) > 1)
			{
				result = true;
				print("  " + (i).toChar);
			}
			i += 1;
		}
		if (result == false)
		{
			println(" No result ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Repetitive = new Repetitive();
		// Test
		task.showDuplicate("complement");
		task.showDuplicate("decoration");
		task.showDuplicate("googlecode");
		task.showDuplicate("abc");
	}
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result
import Foundation;
// Swift 4 program for
// Print duplicate characters from string
class Repetitive
{
	func showDuplicate(_ data: String)
	{
      	let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		var frequence: [Int] = Array(repeating: 0, count: 256);
		var result: Bool = false;
		var i: Int = 0;
		// Count frequence of given text
		while (i < n)
		{
			frequence[Int(UnicodeScalar(String(text[i]))!.value)] += 1;
			i += 1;
		}
		print("\n Given Text : ", data);
		i = 0;
		// Print duplicate characters 
		while (i < 256)
		{
			if (frequence[i] > 1)
			{
				result = true;
              	guard let ch = UnicodeScalar(i) else {
                    return
                }
				print("  ", Character(ch), terminator: "");
			}
			i += 1;
		}
		if (result == false)
		{
			print(" No result ");
		}
	}
}
func main()
{
	let task: Repetitive = Repetitive();
	// Test
	task.showDuplicate("complement");
	task.showDuplicate("decoration");
	task.showDuplicate("googlecode");
	task.showDuplicate("abc");
}
main();

Output

 Given Text :  complement
   e   m
 Given Text :  decoration
   o
 Given Text :  googlecode
   e   g   o
 Given Text :  abc
 No result
// Kotlin program for
// Print duplicate characters from string
class Repetitive
{
	fun showDuplicate(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		// Use to count frequency of possible characters
		val frequence: Array < Int > = Array(256)
		{
			0
		};
		var result: Boolean = false;
		var i: Int = 0;
		// Count frequence of given text
		while (i < n)
		{
			frequence[text.get(i).toInt()] += 1;
			i += 1;
		}
		println("\n Given Text : " + text);
		i = 0;
		// Print duplicate characters 
		while (i < 256)
		{
			if (frequence[i] > 1)
			{
				result = true;
				print("  " + (i).toChar());
			}
			i += 1;
		}
		if (result == false)
		{
			println(" No result ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Repetitive = Repetitive();
	// Test
	task.showDuplicate("complement");
	task.showDuplicate("decoration");
	task.showDuplicate("googlecode");
	task.showDuplicate("abc");
}

Output

 Given Text : complement
  e  m
 Given Text : decoration
  o
 Given Text : googlecode
  e  g  o
 Given Text : abc
 No result


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