Find uncommon characters of the two strings

Here given code implementation process.

// Java Program 
// Find uncommon characters of the two strings
import java.util.HashMap;
public class Distinct
{
	public void uncommon(String x, String y)
	{
		// Auxiliary variable
		int i = 0;
		boolean status = false;
		// Display given text
		System.out.print("\n Given x : " + x);
		System.out.print("\n Given y : " + y);
		System.out.print("\n Result  : ");
		// Use to collect character frequency
		HashMap < Character, Integer > record = new HashMap < Character, Integer > ();
		// Find unique character in first string
		for (i = 0; i < x.length(); i++)
		{
			if (record.containsKey(x.charAt(i)) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record.put(x.charAt(i), 1);
			}
		}
		// Find unique character in second string which is not exist in first
		for (i = 0; i < y.length(); i++)
		{
			if (record.containsKey(y.charAt(i)))
			{
				if (record.get(y.charAt(i)) == 1)
				{
					// Element which is exist in both string
					record.put(y.charAt(i), 3);
				}
			}
			else
			{
				// Unique element in second string 
				// 2 indicates that is a second string character
				record.put(y.charAt(i), 2);
			}
		}
		// Print distinct character
		for (Character key: record.keySet())
		{
			if (record.get(key) != 3)
			{
				System.out.print("  " + key);
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			System.out.print("\n None");
		}
	}
	public static void main(String[] args)
	{
		Distinct task = new Distinct();
		// Test case
		task.uncommon("EDUCATION", "AUTHORIZE");
		task.uncommon("PRECAUTION", "7PRECARIOUS");
	}
}

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   R  C  D  H  Z  N
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   S  T  7  N
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

// C++ Program
// Find uncommon characters of the two strings

class Distinct
{
	public: void uncommon(string x, string y)
	{
		// Auxiliary variable
		int i = 0;
		bool status = false;
		// Display given text
		cout << "\n Given x : " << x;
		cout << "\n Given y : " << y;
		cout << "\n Result  : ";
		// Use to collect character frequency
		unordered_map < char, int > record;
		// Find unique character in first string
		for (i = 0; i < x.length(); i++)
		{
			if (record.find(x[i]) != record.end() == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record[x[i]] = 1;
			}
		}
		// Find unique character in second string which is not exist in first
		for (i = 0; i < y.length(); i++)
		{
			if (record.find(y[i]) != record.end())
			{
				if (record[y[i]] == 1)
				{
					// Element which is exist in both string
					record[y[i]] = 3;
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record[y[i]] = 2;
			}
		}
		for (auto &info: record)
		{
			if (info.second != 3)
			{
				cout << "  " << info.first;
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			cout << "\n None";
		}
	}
};
int main()
{
	Distinct task = Distinct();
	// Test case
	task.uncommon("EDUCATION", "AUTHORIZE");
	task.uncommon("PRECAUTION", "7PRECARIOUS");
	return 0;
}

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   R  Z  C  N  D  H
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   S  7  N  T
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Find uncommon characters of the two strings
public class Distinct
{
	public void uncommon(String x, String y)
	{
		// Auxiliary variable
		int i = 0;
		Boolean status = false;
		// Display given text
		Console.Write("\n Given x : " + x);
		Console.Write("\n Given y : " + y);
		Console.Write("\n Result  : ");
		// Use to collect character frequency
		Dictionary < char, int > record = new Dictionary < char, int > ();
		// Find unique character in first string
		while (i < x.Length)
		{
			if (record.ContainsKey(x[i]) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record.Add(x[i], 1);
			}
			i++;
		}
		i = 0;
		// Find unique character in second string which is not exist in first
		while (i < y.Length)
		{
			if (record.ContainsKey(y[i]))
			{
				if (record[y[i]] == 1)
				{
					// Element which is exist in both string
					record[y[i]] = 3;
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record.Add(y[i], 2);
			}
			i++;
		}
		foreach(KeyValuePair < char, int > info in record)
		{
			if (info.Value != 3)
			{
				Console.Write("  " + info.Key );
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			Console.Write("\n None");
		}
	}
	public static void Main(String[] args)
	{
		Distinct task = new Distinct();
		// Test case
		task.uncommon("EDUCATION", "AUTHORIZE");
		task.uncommon("PRECAUTION", "7PRECARIOUS");
	}
}

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   D  C  N  H  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   T  N  7  S
<?php
// Php Program
// Find uncommon characters of the two strings
class Distinct
{
	public	function uncommon($x, $y)
	{
		// Auxiliary variable
		$i = 0;
		$status = false;
		// Display given text
		echo "\n Given x : ". $x;
		echo "\n Given y : ". $y;
		echo "\n Result  : ";
		// Use to collect character frequency
		$record = array();
		// Find unique character in first string
		for ($i = 0; $i < strlen($x); $i++)
		{
			if (array_key_exists($x[$i], $record) == false)
			{ // Add character
				// 1 indicates that is a first string character
				$record[$x[$i]] = 1;
			}
		}
		// Find unique character in second string which is not exist in first
		for ($i = 0; $i < strlen($y); $i++)
		{
			if (array_key_exists($y[$i], $record))
			{
				if ($record[$y[$i]] == 1)
				{ // Element which is exist in both string
					$record[$y[$i]] = 3;
				}
			}
			else
			{ // Unique element in second string
				// 2 indicates that is a second string character
				$record[$y[$i]] = 2;
			}
		}
		foreach($record as $key => $value)
		{
			if ($value != 3)
			{
				echo "  ". $key;
				// When resultant character exists
				$status = true;
			}
		}
		if ($status == false)
		{
			echo "\n None";
		}
	}
}

function main()
{
	$task = new Distinct();
	$task->uncommon("EDUCATION", "AUTHORIZE");
	$task->uncommon("PRECAUTION", "7PRECARIOUS");
}
main();

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   D  C  N  H  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   T  N  7  S
// Node Js Program
// Find uncommon characters of the two strings
class Distinct
{
	uncommon(x, y)
	{
		// Auxiliary variable
		var i = 0;
		var status = false;
		// Display given text
		process.stdout.write("\n Given x : " + x);
		process.stdout.write("\n Given y : " + y);
		process.stdout.write("\n Result  : ");
      
		// Use to collect character frequency
		var record = new Map();
		// Find unique character in first string
		for (i = 0; i < x.length; i++)
		{
			if (record.has(x.charAt(i)) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record.set(x.charAt(i), 1);
			}
		}
		// Find unique character in second string which is not exist in first
		for (i = 0; i < y.length; i++)
		{
			if (record.has(y.charAt(i)))
			{
				if (record.get(y.charAt(i)) == 1)
				{
					// Element which is exist in both string
					record.set(y.charAt(i), 3);
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record.set(y.charAt(i), 2);
			}
		}
		for (let [key, value] of record)
		{
			if (value != 3)
			{
				process.stdout.write("  " + key);
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			process.stdout.write("\n None");
		}
	}
}

function main()
{
	var task = new Distinct();
	// Test case
	task.uncommon("EDUCATION", "AUTHORIZE");
	task.uncommon("PRECAUTION", "7PRECARIOUS");
}
main();

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   D  C  N  H  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   T  N  7  S
#  Python 3 Program
#  Find uncommon characters of the two strings
class Distinct :
	def uncommon(self, x, y) :
		#  Auxiliary variable
		i = 0
		status = False
		#  Display given text
		print("\n Given x : ", x, end = "")
		print("\n Given y : ", y, end = "")
		print("\n Result  : ", end = "")
		#  Use to collect character frequency
		record = dict()
		i = 0
		#  Find unique character in first string
		while (i < len(x)) :
			if ((x[i] in record.keys()) == False) :
				#  Add character
				#  1 indicates that is a first string character
				record[x[i]] = 1
			i += 1
		i = 0
		#  Find unique character in second string which is not exist in first
		while (i < len(y)) :
			if (y[i] in record.keys()) :
				if (record.get(y[i]) == 1) :
					#  Element which is exist in both string
					record[y[i]] = 3
			else :
				#  Unique element in second string
				#  2 indicates that is a second string character
				record[y[i]] = 2
			
			i += 1
		
		for key, value in record.items() :
			if (value != 3) :
				print("  ", key, end = "")
				#  When resultant character exists
				status = True
			
		
		if (status == False) :
			print("\n None", end = "")
		
	

def main() :
	task = Distinct()
	#  Test case
	task.uncommon("EDUCATION", "AUTHORIZE")
	task.uncommon("PRECAUTION", "7PRECARIOUS")

if __name__ == "__main__": main()

Output

 Given x :  EDUCATION
 Given y :  AUTHORIZE
 Result  :    Z   N   C   R   H   D
 Given x :  PRECAUTION
 Given y :  7PRECARIOUS
 Result  :    T   N   7   S
#  Ruby Program
#  Find uncommon characters of the two strings
class Distinct 
	def uncommon(x, y) 
		#  Auxiliary variable
		i = 0
		status = false
		#  Display given text
		print("\n Given x : ", x)
		print("\n Given y : ", y)
		print("\n Result  : ")
		#  Use to collect character frequency
		record = Hash.new
		i = 0
		#  Find unique character in first string
		while (i < x.length) 
			if (record.key?(x[i]) == false) 
				record[x[i]] = 1
			end

			i += 1
		end

		i = 0
		#  Find unique character in second string which is not exist in first
		while (i < y.length) 
			if (record.key?(y[i])) 
				if (record[y[i]] == 1) 
					record[y[i]] = 3
				end

			else 
				record[y[i]] = 2
			end

			i += 1
		end

		record.each { | key, value | 
			if (record[key] != 3) 
				print("  ", key)
				#  When resultant character exists
				status = true
			end
		}
		if (status == false) 
			print("\n None")
		end

	end

end

def main() 
	task = Distinct.new()
	#  Test case
	task.uncommon("EDUCATION", "AUTHORIZE")
	task.uncommon("PRECAUTION", "7PRECARIOUS")
end

main()

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   D  C  N  H  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   T  N  7  S
import scala.collection.mutable._;
// Scala Program
// Find uncommon characters of the two strings
class Distinct
{
	def uncommon(x: String, y: String): Unit = {
		// Auxiliary variable
		var i: Int = 0;
		var status: Boolean = false;
		// Display given text
		print("\n Given x : " + x);
		print("\n Given y : " + y);
		print("\n Result  : ");
		// Use to collect character frequency
		var record = Map[Char, Int]();
		i = 0;
		// Find unique character in first string
		while (i < x.length())
		{
			if (record.contains(x.charAt(i)) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record.addOne(x.charAt(i), 1);
			}
			i += 1;
		}
		i = 0;
		// Find unique character in second string which is not exist in first
		while (i < y.length())
		{
			if (record.contains(y.charAt(i)))
			{
				if (record.get(y.charAt(i)).get == 1)
				{
					// Element which is exist in both string
					record.addOne(y.charAt(i), 3);
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record.addOne(y.charAt(i), 2);
			}
			i += 1;
		}
		for ((k,v) <- record)
		{
			if (v != 3)
			{
				print("  " + k);
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			print("\n None");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Distinct = new Distinct();
		// Test case
		task.uncommon("EDUCATION", "AUTHORIZE");
		task.uncommon("PRECAUTION", "7PRECARIOUS");
	}
}

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   C  D  H  N  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   N  S  T  7
import Foundation
// Swift 4 Program
// Find uncommon characters of the two strings
class Distinct
{
	func uncommon(_ x1: String, _ y1: String)
	{
      
      	
		// Auxiliary variable
		var i: Int = 0;
		var status: Bool = false;
		// Display given text
		print("\n Given x : ", x1, terminator: "");
		print("\n Given y : ", y1, terminator: "");
		print("\n Result  : ", terminator: "");
      
      	var x = Array(x1);
      	var y = Array(y1);
		// Use to collect character frequency
		var record = [Character: Int]();
		i = 0;
		// Find unique character in first string
		while (i < x.count)
		{
			if ((record.keys.contains(x[i])) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record[x[i]] = 1;
			}
			i += 1;
		}
		i = 0;
		// Find unique character in second string which is not exist in first
		while (i < y.count)
		{
			if (record.keys.contains(y[i]))
			{
				if (record[y[i]] == 1)
				{
					// Element which is exist in both string
					record[y[i]] = 3;
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record[y[i]] = 2;
			}
			i += 1;
		}
		for (key, value) in record
		{
			if (value != 3)
			{
				print("  ", key, terminator: "");
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			print("\n None", terminator: "");
		}
	}
}
func main()
{
	let task: Distinct = Distinct();
	// Test case
	task.uncommon("EDUCATION", "AUTHORIZE");
	task.uncommon("PRECAUTION", "7PRECARIOUS");
}
main();

Output

 Given x :  EDUCATION
 Given y :  AUTHORIZE
 Result  :    N   H   R   C   Z   D
 Given x :  PRECAUTION
 Given y :  7PRECARIOUS
 Result  :    N   S   7   T
// Kotlin Program
// Find uncommon characters of the two strings
class Distinct
{
	fun uncommon(x: String, y: String): Unit
	{
		// Auxiliary variable
		var i: Int = 0;
		var status: Boolean = false;
		// Display given text
		print("\n Given x : " + x);
		print("\n Given y : " + y);
		print("\n Result  : ");
		// Use to collect character frequency

		var record = mutableMapOf < Char , Int > ();
		
		// Find unique character in first string
		while (i < x.length)
		{
			if (record.containsKey(x.get(i)) == false)
			{
				// Add character
				// 1 indicates that is a first string character
				record.put(x.get(i), 1);
			}
			i += 1;
		}
		i = 0;
		// Find unique character in second string which is not exist in first
		while (i < y.length)
		{
			if (record.containsKey(y.get(i)))
			{
				if (record.getValue(y.get(i)) == 1)
				{
					// Element which is exist in both string
					record.put(y.get(i), 3);
				}
			}
			else
			{
				// Unique element in second string
				// 2 indicates that is a second string character
				record.put(y.get(i), 2);
			}
			i += 1;
		}
		// Print distinct character
		for (key in record.keys)
		{
			if (record.getValue(key) != 3)
			{
				print("  " + key);
				// When resultant character exists
				status = true;
			}
		}
		if (status == false)
		{
			print("\n None");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Distinct = Distinct();
	// Test case
	task.uncommon("EDUCATION", "AUTHORIZE");
	task.uncommon("PRECAUTION", "7PRECARIOUS");
}

Output

 Given x : EDUCATION
 Given y : AUTHORIZE
 Result  :   D  C  N  H  R  Z
 Given x : PRECAUTION
 Given y : 7PRECARIOUS
 Result  :   T  N  7  S


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