Skip to main content

Remove those characters from first string which are present in second string

Here given code implementation process.

import java.util.HashSet;
/*
    Java program for
    Remove those characters from first string 
    which are present in second string.
*/
public class Deletion
{
	public void removeString(String str1, String str2)
	{
		String result = "";
		// Use to count frequency of unique characters
		HashSet < Character > record = new HashSet < Character > ();
		// Count frequency of str 2 characters
		for (int i = 0; i < str2.length(); ++i)
		{
			record.add(str2.charAt(i));
		}
		// Collect all characters which is not exist in str2
		for (int i = 0; i < str1.length(); ++i)
		{
			if (!record.contains(str1.charAt(i)))
			{
				result = result + str1.charAt(i);
			}
		}
		// Display given strings
		System.out.println(" Given str1 : " + str1);
		System.out.println(" Given str2 : " + str2);
		// Display calculated result
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		Deletion task = new Deletion();
		String str1 = "allnewfriends";
		String str2 = "idea";
		// Test
		task.removeString(str1, str2);
	}
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
// Include header file
#include <iostream>
#include <set>
#include <string>

using namespace std;
/*
    C++ program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion
{
	public: void removeString(string str1, string str2)
	{
		string result = "";
		// Use to count frequency of unique characters
		set < char > record;
		// Count frequency of str 2 characters
		for (int i = 0; i < str2.length(); ++i)
		{
			record.insert(str2[i]);
		}
		// Collect all characters which is not exist in str2
		for (int i = 0; i < str1.length(); ++i)
		{
			if (record.find(str1[i]) == record.end())
			{
				result = result  +  str1[i];
			}
		}
		// Display given strings
		cout << " Given str1 : " << str1 << endl;
		cout << " Given str2 : " << str2 << endl;
		// Display calculated result
		cout << " Result : " << result << endl;
	}
};
int main()
{
	Deletion *task = new Deletion();
	string str1 = "allnewfriends";
	string str2 = "idea";
	// Test
	task->removeString(str1, str2);
	return 0;
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program for
    Remove those characters from first string 
    which are present in second string.
*/
public class Deletion
{
	public void removeString(String str1, String str2)
	{
		String result = "";
		// Use to count frequency of unique characters
		HashSet < char > record = new HashSet < char > ();
		// Count frequency of str 2 characters
		for (int i = 0; i < str2.Length; ++i)
		{
			record.Add(str2[i]);
		}
		// Collect all characters which is not exist in str2
		for (int i = 0; i < str1.Length; ++i)
		{
			if (!record.Contains(str1[i]))
			{
				result = result + str1[i];
			}
		}
		// Display given strings
		Console.WriteLine(" Given str1 : " + str1);
		Console.WriteLine(" Given str2 : " + str2);
		// Display calculated result
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		Deletion task = new Deletion();
		String str1 = "allnewfriends";
		String str2 = "idea";
		// Test
		task.removeString(str1, str2);
	}
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
package main
import "fmt"
/*
    Go program for
    Remove those characters from first string 
    which are present in second string.
*/
func removeString(str1, str2 string) {
	var result string = ""
	// Use to count frequency of unique characters
	var record = make(map[byte] bool)
	// Count frequency of str 2 characters
	for i := 0 ; i < len(str2) ; i++ {
		record[str2[i]] = true
	}
	// Collect all characters which is not exist in str2
	for i := 0 ; i < len(str1) ; i++ {
		if _, found := record[str1[i]] ; !found {
			result = result + string(str1[i])
		}
	}
	// Display given strings
	fmt.Println(" Given str1 : ", str1)
	fmt.Println(" Given str2 : ", str2)
	// Display calculated result
	fmt.Println(" Result : ", result)
}
func main() {

	var str1 string = "allnewfriends"
	var str2 string = "idea"
	// Test
	removeString(str1, str2)
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
<?php
/*
    Php program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion
{
	public	function removeString($str1, $str2)
	{
		$result = "";
		// Use to count frequency of unique characters
		$record = array();
		// Count frequency of str 2 characters
		for ($i = 0; $i < strlen($str2); ++$i)
		{
			if (!in_array($str2[$i], $record))
			{
				$record[] = $str2[$i];
            }
		}
		// Collect all characters which is not exist in str2
		for ($i = 0; $i < strlen($str1); ++$i)
		{
			if (!in_array($str1[$i], $record, TRUE))
			{
				$result = $result.$str1[$i];
			}
		}
		// Display given strings
		echo(" Given str1 : ".$str1."\n");
		echo(" Given str2 : ".$str2."\n");
		// Display calculated result
		echo(" Result : ".$result."\n");
	}
}

function main()
{
	$task = new Deletion();
	$str1 = "allnewfriends";
	$str2 = "idea";
	// Test
	$task->removeString($str1, $str2);
}
main();

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
/*
    Node JS program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion
{
	removeString(str1, str2)
	{
		var result = "";
		// Use to count frequency of unique characters
		var record = new Set();
		// Count frequency of str 2 characters
		for (var i = 0; i < str2.length; ++i)
		{
			record.add(str2.charAt(i));
		}
		// Collect all characters which is not exist in str2
		for (var i = 0; i < str1.length; ++i)
		{
			if (!record.has(str1.charAt(i)))
			{
				result = result + str1.charAt(i);
			}
		}
		// Display given strings
		console.log(" Given str1 : " + str1);
		console.log(" Given str2 : " + str2);
		// Display calculated result
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new Deletion();
	var str1 = "allnewfriends";
	var str2 = "idea";
	// Test
	task.removeString(str1, str2);
}
main();

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
#    Python 3 program for
#    Remove those characters from first string 
#    which are present in second string.
class Deletion :
	def removeString(self, str1, str2) :
		result = ""
		#  Use to count frequency of unique characters
		record = set()
		i = 0
		#  Count frequency of str 2 characters
		while (i < len(str2)) :
			record.add(str2[i])
			i += 1
		
		i = 0
		#  Collect all characters which is not exist in str2
		while (i < len(str1)) :
			if (not str1[i] in record) :
				result = result + str(str1[i])
			
			i += 1
		
		#  Display given strings
		print(" Given str1 : ", str1)
		print(" Given str2 : ", str2)
		#  Display calculated result
		print(" Result : ", result)
	

def main() :
	task = Deletion()
	str1 = "allnewfriends"
	str2 = "idea"
	#  Test
	task.removeString(str1, str2)

if __name__ == "__main__": main()

Output

 Given str1 :  allnewfriends
 Given str2 :  idea
 Result :  llnwfrns
require 'set'
#    Ruby program for
#    Remove those characters from first string 
#    which are present in second string.
class Deletion 
	def removeString(str1, str2) 
		result = ""
		#  Use to count frequency of unique characters
		record = SortedSet.new()
		i = 0
		#  Count frequency of str 2 characters
		while (i < str2.length) 
			record.add(str2[i])
			i += 1
		end

		i = 0
		#  Collect all characters which is not exist in str2
		while (i < str1.length) 
			if (!record.include?(str1[i])) 
				result = result + str1[i].to_s
			end

			i += 1
		end

		#  Display given strings
		print(" Given str1 : ", str1, "\n")
		print(" Given str2 : ", str2, "\n")
		#  Display calculated result
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = Deletion.new()
	str1 = "allnewfriends"
	str2 = "idea"
	#  Test
	task.removeString(str1, str2)
end

main()

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
import scala.collection.mutable._;
/*
    Scala program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion()
{
	def removeString(str1: String, str2: String): Unit = {
		var result: String = "";
		// Use to count frequency of unique characters
		var record: Set[Character] = Set();
		var i: Int = 0;
		// Count frequency of str 2 characters
		while (i < str2.length())
		{
			record.add(str2.charAt(i));
			i += 1;
		}
		i = 0;
		// Collect all characters which is not exist in str2
		while (i < str1.length())
		{
			if (!record.contains(str1.charAt(i)))
			{
				result = result + str1.charAt(i).toString();
			}
			i += 1;
		}
		// Display given strings
		println(" Given str1 : " + str1);
		println(" Given str2 : " + str2);
		// Display calculated result
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Deletion = new Deletion();
		var str1: String = "allnewfriends";
		var str2: String = "idea";
		// Test
		task.removeString(str1, str2);
	}
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns
import Foundation;
/*
    Swift 4 program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion
{
	func removeString(_ a: String, _ b: String)
	{
      	let str1 = Array(a);
        let str2 = Array(b);
		var result: String = "";
		// Use to count frequency of unique characters
		var record = Set<Character>();
		var i: Int = 0;
		// Count frequency of str 2 characters
		while (i < str2.count)
		{
			record.insert(str2[i]);
			i += 1;
		}
		i = 0;
		// Collect all characters which is not exist in str2
		while (i < str1.count)
		{
			if (!record.contains(str1[i]))
			{
				result = result + String(str1[i]);
			}
			i += 1;
		}
		// Display given strings
		print(" Given str1 : ", a);
		print(" Given str2 : ", b);
		// Display calculated result
		print(" Result : ", result);
	}
}
func main()
{
	let task: Deletion = Deletion();
	let str1: String = "allnewfriends";
	let str2: String = "idea";
	// Test
	task.removeString(str1, str2);
}
main();

Output

 Given str1 :  allnewfriends
 Given str2 :  idea
 Result :  llnwfrns
/*
    Kotlin program for
    Remove those characters from first string 
    which are present in second string.
*/
class Deletion
{
	fun removeString(str1: String, str2: String): Unit
	{
		var result: String = "";
		// Use to count frequency of unique characters
		var record = mutableSetOf< Char > ();
		var i: Int = 0;
		// Count frequency of str 2 characters
		while (i < str2.length)
		{
			record.add(str2.get(i));
			i += 1;
		}
		i = 0;
		// Collect all characters which is not exist in str2
		while (i < str1.length)
		{
			if (!record.contains(str1.get(i)))
			{
				result = result + str1.get(i).toString();
			}
			i += 1;
		}
		// Display given strings
		println(" Given str1 : " + str1);
		println(" Given str2 : " + str2);
		// Display calculated result
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Deletion = Deletion();
	val str1: String = "allnewfriends";
	val str2: String = "idea";
	// Test
	task.removeString(str1, str2);
}

Output

 Given str1 : allnewfriends
 Given str2 : idea
 Result : llnwfrns




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