Skip to main content

Count number of unique characters in a string

Here given code implementation process.

/*
    C program for
    Count number of unique characters in a string
*/
#include <stdio.h>
#include <string.h>

void countUniqueChar(char *text)
{
	int n = strlen(text);
	if (n == 0)
	{
		return;
	}
	int result = 0;
	int record[256];
	// Set initial count of character record
	for (int i = 0; i < 256; ++i)
	{
		record[i] = 0;
	}
	// Count frequency of character element
	for (int i = 0; i < n; ++i)
	{
		record[text[i]] = record[text[i]] + 1;
	}
	for (int i = 0; i < n; ++i)
	{
		if (record[text[i]] == 1)
		{
			// When [i] location character appears  once
			result++;
		}
	}
	printf("\n Given Text : %s", text);
	printf("\n Number of unique characters is %d", result);
}
int main(int argc, char
	const *argv[])
{
	// Given Sting text
	char *text1 = "office";
	char *text2 = "error";
	char *text3 = "xxxx";
	// Test
	countUniqueChar(text1);
	countUniqueChar(text2);
	countUniqueChar(text3);
	return 0;
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
/*
    Java program for
    Count number of unique characters in a string
*/
public class NonRepetitive
{
    public void countUniqueChar(String text)
    {
        int n = text.length();
        if (n == 0)
        {
            return;
        }
        int result = 0;
        int[] record = new int[256];
        // Set initial count of character record
        for (int i = 0; i < 256; ++i)
        {
            record[i] = 0;
        }
        // Count frequency of character element
        for (int i = 0; i < n; ++i)
        {
            record[text.charAt(i)] += 1;
        }
        for (int i = 0; i < n; ++i)
        {
            if (record[text.charAt(i)] == 1)
            {
                // When [i] location character appears  once
                result++;
            }
        }
        System.out.print("\n Given Text : " + text);
        System.out.print("\n Number of unique characters is " + result );
    }
    public static void main(String[] args)
    {
        NonRepetitive task = new NonRepetitive();

        // Given Sting text
        String text1 = "office";
        String text2 = "error";
        String text3 = "xxxx";
        // Test
        task.countUniqueChar(text1);
        task.countUniqueChar(text2);
        task.countUniqueChar(text3);

    }
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Count number of unique characters in a string
*/
class NonRepetitive
{
	public: void countUniqueChar(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		int result = 0;
		int record[256];
		// Set initial count of character record
		for (int i = 0; i < 256; ++i)
		{
			record[i] = 0;
		}
		// Count frequency of character element
		for (int i = 0; i < n; ++i)
		{
			record[text[i]] += 1;
		}
		for (int i = 0; i < n; ++i)
		{
			if (record[text[i]] == 1)
			{
				// When [i] location character appears  once
				result++;
			}
		}
		cout << "\n Given Text : " << text;
		cout << "\n Number of unique characters is " << result;
	}
};
int main()
{
	NonRepetitive *task = new NonRepetitive();
	// Given Sting text
	string text1 = "office";
	string text2 = "error";
	string text3 = "xxxx";
	// Test
	task->countUniqueChar(text1);
	task->countUniqueChar(text2);
	task->countUniqueChar(text3);
	return 0;
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
// Include namespace system
using System;
/*
    Csharp program for
    Count number of unique characters in a string
*/
public class NonRepetitive
{
	public void countUniqueChar(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		int result = 0;
		int[] record = new int[256];
		// Set initial count of character record
		for (int i = 0; i < 256; ++i)
		{
			record[i] = 0;
		}
		// Count frequency of character element
		for (int i = 0; i < n; ++i)
		{
			record[text[i]] += 1;
		}
		for (int i = 0; i < n; ++i)
		{
			if (record[text[i]] == 1)
			{
				// When [i] location character appears  once
				result++;
			}
		}
		Console.Write("\n Given Text : " + text);
		Console.Write("\n Number of unique characters is " + result);
	}
	public static void Main(String[] args)
	{
		NonRepetitive task = new NonRepetitive();
		// Given Sting text
		String text1 = "office";
		String text2 = "error";
		String text3 = "xxxx";
		// Test
		task.countUniqueChar(text1);
		task.countUniqueChar(text2);
		task.countUniqueChar(text3);
	}
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
package main
import "fmt"
/*
    Go program for
    Count number of unique characters in a string
*/

func countUniqueChar(text string) {
	var n int = len(text)
	if n == 0 {
		return
	}
	var result int = 0
	var record = make([] int,256)
	// Count frequency of character element
	for i := 0 ; i < n ; i++ {
		record[text[i]] += 1
	}
	for i := 0 ; i < n ; i++ {
		if record[text[i]] == 1 {
			// When [i] location character appears  once
			result++
		}
	}
	fmt.Print("\n Given Text : ", text)
	fmt.Print("\n Number of unique characters is ", result)
}
func main() {

	// Given Sting text
	var text1 string = "office"
	var text2 string = "error"
	var text3 string = "xxxx"
	// Test
	countUniqueChar(text1)
	countUniqueChar(text2)
	countUniqueChar(text3)
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
<?php
/*
    Php program for
    Count number of unique characters in a string
*/
class NonRepetitive
{
	public	function countUniqueChar($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		$result = 0;
		$record = array_fill(0, 256, 0);
		// Count frequency of character element
		for ($i = 0; $i < $n; ++$i)
		{
			$record[ord($text[$i])] += 1;
		}
		for ($i = 0; $i < $n; ++$i)
		{
			if ($record[ord($text[$i])] == 1)
			{
				// When [i] location character appears  once
				$result++;
			}
		}
		echo("\n Given Text : ".$text);
		echo("\n Number of unique characters is ".$result);
	}
}

function main()
{
	$task = new NonRepetitive();
	// Given Sting text
	$text1 = "office";
	$text2 = "error";
	$text3 = "xxxx";
	// Test
	$task->countUniqueChar($text1);
	$task->countUniqueChar($text2);
	$task->countUniqueChar($text3);
}
main();

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
/*
    Node JS program for
    Count number of unique characters in a string
*/
class NonRepetitive
{
	countUniqueChar(text)
	{
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		var result = 0;
		var record = Array(256).fill(0);
		// Count frequency of character element
		for (var i = 0; i < n; ++i)
		{
			record[text.charCodeAt(i)] += 1;
		}
		for (var i = 0; i < n; ++i)
		{
			if (record[text.charCodeAt(i)] == 1)
			{
				// When [i] location character appears  once
				result++;
			}
		}
		process.stdout.write("\n Given Text : " + text);
		process.stdout.write("\n Number of unique characters is " + result);
	}
}

function main()
{
	var task = new NonRepetitive();
	// Given Sting text
	var text1 = "office";
	var text2 = "error";
	var text3 = "xxxx";
	// Test
	task.countUniqueChar(text1);
	task.countUniqueChar(text2);
	task.countUniqueChar(text3);
}
main();

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
#    Python 3 program for
#    Count number of unique characters in a string
class NonRepetitive :
	def countUniqueChar(self, text) :
		n = len(text)
		if (n == 0) :
			return
		
		result = 0
		record = [0] * (256)
		i = 0
		#  Count frequency of character element
		while (i < n) :
			record[ord(text[i])] += 1
			i += 1
		
		i = 0
		while (i < n) :
			if (record[ord(text[i])] == 1) :
				#  When [i] location character appears  once
				result += 1
			
			i += 1
		
		print("\n Given Text : ", text, end = "")
		print("\n Number of unique characters is ", result, end = "")
	

def main() :
	task = NonRepetitive()
	#  Given Sting text
	text1 = "office"
	text2 = "error"
	text3 = "xxxx"
	#  Test
	task.countUniqueChar(text1)
	task.countUniqueChar(text2)
	task.countUniqueChar(text3)

if __name__ == "__main__": main()

Output

 Given Text :  office
 Number of unique characters is  4
 Given Text :  error
 Number of unique characters is  2
 Given Text :  xxxx
 Number of unique characters is  0
#    Ruby program for
#    Count number of unique characters in a string
class NonRepetitive 
	def countUniqueChar(text) 
		n = text.length
		if (n == 0) 
			return
		end

		result = 0
		record = Array.new(256) {0}
		i = 0
		#  Count frequency of character element
		while (i < n) 
			record[text[i].ord] += 1
			i += 1
		end

		i = 0
		while (i < n) 
			if (record[text[i].ord] == 1) 
				#  When [i] location character appears  once
				result += 1
			end

			i += 1
		end

		print("\n Given Text : ", text)
		print("\n Number of unique characters is ", result)
	end

end

def main() 
	task = NonRepetitive.new()
	#  Given Sting text
	text1 = "office"
	text2 = "error"
	text3 = "xxxx"
	#  Test
	task.countUniqueChar(text1)
	task.countUniqueChar(text2)
	task.countUniqueChar(text3)
end

main()

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
/*
    Scala program for
    Count number of unique characters in a string
*/
class NonRepetitive()
{
	def countUniqueChar(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		var result: Int = 0;
		var record: Array[Int] = Array.fill[Int](256)(0);
		var i: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
			record(text.charAt(i)) += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (record(text.charAt(i)) == 1)
			{
				// When [i] location character appears  once
				result += 1;
			}
			i += 1;
		}
		print("\n Given Text : " + text);
		print("\n Number of unique characters is " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NonRepetitive = new NonRepetitive();
		// Given Sting text
		var text1: String = "office";
		var text2: String = "error";
		var text3: String = "xxxx";
		// Test
		task.countUniqueChar(text1);
		task.countUniqueChar(text2);
		task.countUniqueChar(text3);
	}
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0
import Foundation;
/*
    Swift 4 program for
    Count number of unique characters in a string
*/
class NonRepetitive
{
	func countUniqueChar(_ data: String)
	{
      	let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		var result: Int = 0;
		var record: [Int] = Array(repeating: 0, count: 256);
		var i: Int = 0;
      	var index: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
          	index = Int(UnicodeScalar(String(text[i]))!.value);
			record[index] += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
          	index = Int(UnicodeScalar(String(text[i]))!.value);
			if (record[index] == 1)
			{
				// When [i] location character appears  once
				result += 1;
			}
			i += 1;
		}
		print("\n Given Text : ", data, terminator: "");
		print("\n Number of unique characters is ", result, terminator: "");
	}
}
func main()
{
	let task: NonRepetitive = NonRepetitive();
	// Given Sting text
	let text1: String = "office";
	let text2: String = "error";
	let text3: String = "xxxx";
	// Test
	task.countUniqueChar(text1);
	task.countUniqueChar(text2);
	task.countUniqueChar(text3);
}
main();

Output

 Given Text :  office
 Number of unique characters is  4
 Given Text :  error
 Number of unique characters is  2
 Given Text :  xxxx
 Number of unique characters is  0
/*
    Kotlin program for
    Count number of unique characters in a string
*/
class NonRepetitive
{
	fun countUniqueChar(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		var result: Int = 0;
		val record: Array < Int > = Array(256)
		{
			0
		};
		var i: Int = 0;
		// Count frequency of character element
		while (i < n)
		{
			record[text.get(i).toInt()] += 1;
			i += 1;
		}
		i = 0;
		while (i < n)
		{
			if (record[text.get(i).toInt()] == 1)
			{
				// When [i] location character appears  once
				result += 1;
			}
			i += 1;
		}
		print("\n Given Text : " + text);
		print("\n Number of unique characters is " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NonRepetitive = NonRepetitive();
	// Given Sting text
	val text1: String = "office";
	val text2: String = "error";
	val text3: String = "xxxx";
	// Test
	task.countUniqueChar(text1);
	task.countUniqueChar(text2);
	task.countUniqueChar(text3);
}

Output

 Given Text : office
 Number of unique characters is 4
 Given Text : error
 Number of unique characters is 2
 Given Text : xxxx
 Number of unique characters is 0




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