Skip to main content

Find the first non-repeating character of string

Here given code implementation process.

/*
  Java Program for 
  Find the first non-repeating character of string
*/
public class Frequency
{
	void firstNonRepeat(String text)
	{
		System.out.println(" Given text : " + text);
		// Used to collect character frequency
		int[] count = new int[256];
		// Loop controlling variable
		int i;
		// Set initial character frequency
		for (i = 0; i < 256; i++)
		{
			count[i] = 0;
		}
		// Count character frequency
		for (i = 0; i < text.length(); i++)
		{
			count[text.charAt(i)]++;
		}
		// Find first non-repeating character
		for (i = 0; i < text.length(); i++)
		{
			if (count[text.charAt(i)] == 1)
			{
				// When we get first non-repeating character
				System.out.print(" First non-repeating character : ");
				System.out.println(text.charAt(i));
				return;
			}
		}
		// When string contains duplicate characters
		System.out.println(" First non-repeating character not exist ");
	}
	public static void main(String[] args)
	{
		Frequency task = new Frequency();
		// Test Case
		task.firstNonRepeat("abcaXbyyz");
		task.firstNonRepeat("aeabbccefzs");
		task.firstNonRepeat("abcxyzzabxcy");
		task.firstNonRepeat("a+b/a-c");
	}
}

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
// Include header file
#include <iostream>

#include <string>

using namespace std;
/*
  C++ Program for 
  Find the first non-repeating character of string
*/
class Frequency
{
	public: void firstNonRepeat(string text)
	{
		cout << " Given text : " << text << endl;
		// Used to collect character frequency
		int count[256];
		// Loop controlling variable
		int i;
		// Set initial character frequency
		for (i = 0; i < 256; i++)
		{
			count[i] = 0;
		}
		// Count character frequency
		for (i = 0; i < text.length(); i++)
		{
			count[text[i]]++;
		}
		// Find first non-repeating character
		for (i = 0; i < text.length(); i++)
		{
			if (count[text[i]] == 1)
			{
				// When we get first non-repeating character
				cout << " First non-repeating character : ";
				cout << text[i] << endl;
				return;
			}
		}
		// When string contains duplicate characters
		cout << " First non-repeating character not exist " << endl;
	}
};
int main()
{
	Frequency *task = new Frequency();
	// Test Case
	task->firstNonRepeat("abcaXbyyz");
	task->firstNonRepeat("aeabbccefzs");
	task->firstNonRepeat("abcxyzzabxcy");
	task->firstNonRepeat("a+b/a-c");
	return 0;
}

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
// Include namespace system
using System;
/*
  Csharp Program for 
  Find the first non-repeating character of string
*/
public class Frequency
{
	void firstNonRepeat(String text)
	{
		Console.WriteLine(" Given text : " + text);
		// Used to collect character frequency
		int[] count = new int[256];
		// Loop controlling variable
		int i;
		// Set initial character frequency
		for (i = 0; i < 256; i++)
		{
			count[i] = 0;
		}
		// Count character frequency
		for (i = 0; i < text.Length; i++)
		{
			count[text[i]]++;
		}
		// Find first non-repeating character
		for (i = 0; i < text.Length; i++)
		{
			if (count[text[i]] == 1)
			{
				// When we get first non-repeating character
				Console.Write(" First non-repeating character : ");
				Console.WriteLine(text[i]);
				return;
			}
		}
		// When string contains duplicate characters
		Console.WriteLine(" First non-repeating character not exist ");
	}
	public static void Main(String[] args)
	{
		Frequency task = new Frequency();
		// Test Case
		task.firstNonRepeat("abcaXbyyz");
		task.firstNonRepeat("aeabbccefzs");
		task.firstNonRepeat("abcxyzzabxcy");
		task.firstNonRepeat("a+b/a-c");
	}
}

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
<?php
/*
  Php Program for 
  Find the first non-repeating character of string
*/
class Frequency
{
	function firstNonRepeat($text)
	{
		echo " Given text : ".$text.
		"\n";
		// Used to collect character frequency
		$count = array_fill(0, 256, 0);
		// Loop controlling variable
		$i = 0;
		// Count character frequency
		for ($i = 0; $i < strlen($text); $i++)
		{
			$count[ord($text[$i])]++;
		}
		// Find first non-repeating character
		for ($i = 0; $i < strlen($text); $i++)
		{
			if ($count[ord($text[$i])] == 1)
			{
				// When we get first non-repeating character
				echo " First non-repeating character : ";
				echo $text[$i]."\n";
				return;
			}
		}
		// When string contains duplicate characters
		echo " First non-repeating character not exist ".
		"\n";
	}
}

function main()
{
	$task = new Frequency();
	// Test Case
	$task->firstNonRepeat("abcaXbyyz");
	$task->firstNonRepeat("aeabbccefzs");
	$task->firstNonRepeat("abcxyzzabxcy");
	$task->firstNonRepeat("a+b/a-c");
}
main();

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
/*
  Node JS Program for 
  Find the first non-repeating character of string
*/
class Frequency
{
	firstNonRepeat(text)
	{
		console.log(" Given text : " + text);
		// Used to collect character frequency
		var count = Array(256).fill(0);
		// Loop controlling variable
		var i = 0;
		// Count character frequency
		for (i = 0; i < text.length; i++)
		{
			count[text.charCodeAt(i)]++;
		}
		// Find first non-repeating character
		for (i = 0; i < text.length; i++)
		{
			if (count[text.charCodeAt(i)] == 1)
			{
				// When we get first non-repeating character
				process.stdout.write(" First non-repeating character : ");
				console.log(text.charAt(i));
				return;
			}
		}
		// When string contains duplicate characters
		console.log(" First non-repeating character not exist ");
	}
}

function main()
{
	var task = new Frequency();
	// Test Case
	task.firstNonRepeat("abcaXbyyz");
	task.firstNonRepeat("aeabbccefzs");
	task.firstNonRepeat("abcxyzzabxcy");
	task.firstNonRepeat("a+b/a-c");
}
main();

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
#  Python 3 Program for 
#  Find the first non-repeating character of string
class Frequency :
	def firstNonRepeat(self, text) :
		print(" Given text : ", text)
		count = [0] * (256)
		i = 0
		#  Count character frequency
		i = 0
		while (i < len(text)) :
			count[ord(text[i])] += 1
			i += 1
		
		#  Find first non-repeating character
		i = 0
		while (i < len(text)) :
			if (count[ord(text[i])] == 1) :
				#  When we get first non-repeating character
				print(" First non-repeating character : ", end = "")
				print(text[i])
				return
			
			i += 1
		
		#  When string contains duplicate characters
		print(" First non-repeating character not exist ")
	

def main() :
	task = Frequency()
	#  Test Case
	task.firstNonRepeat("abcaXbyyz")
	task.firstNonRepeat("aeabbccefzs")
	task.firstNonRepeat("abcxyzzabxcy")
	task.firstNonRepeat("a+b/a-c")

if __name__ == "__main__": main()

input

 Given text :  abcaXbyyz
 First non-repeating character : c
 Given text :  aeabbccefzs
 First non-repeating character : f
 Given text :  abcxyzzabxcy
 First non-repeating character not exist
 Given text :  a+b/a-c
 First non-repeating character : +
#  Ruby Program for 
#  Find the first non-repeating character of string
class Frequency 
	def firstNonRepeat(text) 
		print(" Given text : ", text, "\n")
		#  Used to collect character frequency
		count = Array.new(256) {0}
		#  Loop controlling variable
		i = 0
		#  Count character frequency
		i = 0
		while (i < text.length) 
			count[text[i].ord] += 1
			i += 1
		end

		#  Find first non-repeating character
		i = 0
		while (i < text.length) 
			if (count[text[i].ord] == 1) 
				#  When we get first non-repeating character
				print(" First non-repeating character : ")
				print(text[i], "\n")
				return
			end

			i += 1
		end

		#  When string contains duplicate characters
		print(" First non-repeating character not exist ", "\n")
	end

end

def main() 
	task = Frequency.new()
	#  Test Case
	task.firstNonRepeat("abcaXbyyz")
	task.firstNonRepeat("aeabbccefzs")
	task.firstNonRepeat("abcxyzzabxcy")
	task.firstNonRepeat("a+b/a-c")
end

main()

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist 
 Given text : a+b/a-c
 First non-repeating character : +
/*
  Scala Program for 
  Find the first non-repeating character of string
*/
class Frequency()
{
	def firstNonRepeat(text: String): Unit = {
		println(" Given text : " + text);
		// Used to collect character frequency
		var count: Array[Int] = Array.fill[Int](256)(0);
		// Loop controlling variable
		var i: Int = 0;
		// Count character frequency
		i = 0;
		while (i < text.length())
		{
			count(text.charAt(i).toInt) += 1;
			i += 1;
		}
		// Find first non-repeating character
		i = 0;
		while (i < text.length())
		{
			if (count(text.charAt(i).toInt) == 1)
			{
				// When we get first non-repeating character
				print(" First non-repeating character : ");
				println(text.charAt(i));
				return;
			}
			i += 1;
		}
		// When string contains duplicate characters
		println(" First non-repeating character not exist ");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Frequency = new Frequency();
		// Test Case
		task.firstNonRepeat("abcaXbyyz");
		task.firstNonRepeat("aeabbccefzs");
		task.firstNonRepeat("abcxyzzabxcy");
		task.firstNonRepeat("a+b/a-c");
	}
}

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +
/*
  Swift 4 Program for 
  Find the first non-repeating character of string
*/
class Frequency
{
	func firstNonRepeat(_ data: String)
	{
        let text = Array(data);
		print(" Given text : ", data);
		// Used to collect character frequency
		var count: [Int] = Array(repeating: 0, count: 256);
		// Loop controlling variable
		var i: Int = 0;
		// Count character frequency
		while (i < text.count)
		{
            let position = Int(UnicodeScalar(String(text[i]))!.value);
			count[position] += 1;
			i += 1;
		}
		// Find first non-repeating character
		i = 0;
		while (i < text.count)
		{
          	let position = Int(UnicodeScalar(String(text[i]))!.value);
			if (count[position] == 1)
			{
				// When we get first non-repeating character
				print(" First non-repeating character : ", terminator: "");
				print(text[i]);
				return;
			}
			i += 1;
		}
		// When string contains duplicate characters
		print(" First non-repeating character not exist ");
	}
}
func main()
{
	let task: Frequency = Frequency();
	// Test Case
	task.firstNonRepeat("abcaXbyyz");
	task.firstNonRepeat("aeabbccefzs");
	task.firstNonRepeat("abcxyzzabxcy");
	task.firstNonRepeat("a+b/a-c");
}
main();

input

 Given text :  abcaXbyyz
 First non-repeating character : c
 Given text :  aeabbccefzs
 First non-repeating character : f
 Given text :  abcxyzzabxcy
 First non-repeating character not exist
 Given text :  a+b/a-c
 First non-repeating character : +
/*
  Kotlin Program for 
  Find the first non-repeating character of string
*/
class Frequency
{
	fun firstNonRepeat(text: String): Unit
	{
		println(" Given text : " + text);
		// Used to collect character frequency
		val count: Array < Int > = Array(256)
		{
			0
		};
		// Loop controlling variable
		var i: Int = 0;
		while (i < text.length)
		{
			count[text.get(i).toInt()] += 1;
			i += 1;
		}
		i = 0;
		while (i < text.length)
		{
			if (count[text.get(i).toInt()] == 1)
			{
				// When we get first non-repeating character
				print(" First non-repeating character : ");
				println(text.get(i));
				return;
			}
			i += 1;
		}
		// When string contains duplicate characters
		println(" First non-repeating character not exist ");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Frequency = Frequency();
	// Test Case
	task.firstNonRepeat("abcaXbyyz");
	task.firstNonRepeat("aeabbccefzs");
	task.firstNonRepeat("abcxyzzabxcy");
	task.firstNonRepeat("a+b/a-c");
}

input

 Given text : abcaXbyyz
 First non-repeating character : c
 Given text : aeabbccefzs
 First non-repeating character : f
 Given text : abcxyzzabxcy
 First non-repeating character not exist
 Given text : a+b/a-c
 First non-repeating character : +




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