Skip to main content

Remove duplicate words from string

Here given code implementation process.

// Include header file
#include <iostream>
#include <set>
#include <string.h>

using namespace std;
/*
    C++ program
    Remove duplicate words from string
*/
class Finding
{
	public: void findRepeatedwords(char *text)
	{
      	string data = text;
		// Collect all words of string
		char *words = strtok(text, " ");
		// Using of this to collect words and display status.
		set < string > record;
		
		string result = "";
		// Execute loop to check all words
		while (words != NULL)
		{
			if (record.find(words) == record.end())
			{
				if (result.compare("") == 0)
				{
					// Add word
					result += words;
				}
				else
				{
                  	
					// Add space and word
                  	result += " ";
					result += words;
				}
				record.insert(words);
			}
          	words = strtok(NULL, " ");
		}
		cout << " Input  : " << data << endl;
		cout << " Result : " << result << endl;
	}
};
int main()
{
	Finding *task = new Finding();
	char text[] = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
	task->findRepeatedwords(text);
	return 0;
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
import java.util.HashSet;
/*
    Java program
    Remove duplicate words from string
*/
public class Finding
{
	public void findRepeatedwords(String text)
	{
		// Collect all words of string
		String[] words = text.split(" ");
		// Using of this to collect words and display status.
		HashSet < String > record = new HashSet < String > ();
		int n = words.length;
		String result = "";
		// Execute loop to check all words
		for (int i = 0; i < n; i++)
		{
			if (!record.contains(words[i]))
			{
				if (result.equals(""))
				{
					// Add word
					result += words[i];
				}
				else
				{
					// Add space and word
					result += " " + words[i];
				}
				record.add(words[i]);
			}
		}
		System.out.println(" Input  : " + text);
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		Finding task = new Finding();
		String text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
		task.findRepeatedwords(text);
	}
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program
    Remove duplicate words from string
*/
public class Finding
{
	public void findRepeatedwords(String text)
	{
		// Collect all words of string
		String[] words = text.Split(" ");
		// Using of this to collect words and display status.
		HashSet < string > record = new HashSet < string > ();
		int n = words.Length;
		String result = "";
		// Execute loop to check all words
		for (int i = 0; i < n; i++)
		{
			if (!record.Contains(words[i]))
			{
				if (result.Equals(""))
				{
					// Add word
					result += words[i];
				}
				else
				{
					// Add space and word
					result += " " + words[i];
				}
				record.Add(words[i]);
			}
		}
		Console.WriteLine(" Input  : " + text);
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		Finding task = new Finding();
		String text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
		task.findRepeatedwords(text);
	}
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
package main
import "strings"
import "fmt"
/*
    Go program
    Remove duplicate words from string
*/

func  findRepeatedwords(text string) {
	// Collect all words of string
	var words = strings.Split(text," ")
	// Using of this to collect words and display status.
	var record = make(map[string] bool)
	var n int = len(words)
	var result string = ""
	// Execute loop to check all words
	for i := 0 ; i < n ; i++ {
		if _, found := record[words[i]] ; found==false {
			if result == "" {
				// Add word
				result += words[i]
			} else {
				// Add space and word
				result += " " + words[i]
			}
			record[words[i]] = true
		}
	}
	fmt.Println(" Input  : ", text)
	fmt.Println(" Result : ", result)
}
func main() {

	var text string = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple"
	findRepeatedwords(text)
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
<?php
/*
    Php program
    Remove duplicate words from string
*/
class Finding
{
	public	function findRepeatedwords($text)
	{
		// Collect all words of string
		$words = explode(" ", $text);
		// Using of this to collect words and display status.
		$record = array();
		$n = count($words);
		$result = "";
		// Execute loop to check all words
		for ($i = 0; $i < $n; $i++)
		{
			if (!in_array($words[$i], $record, TRUE))
			{
				if ((strcmp($result, "") == 0))
				{
					// Add word
					$result .= $words[$i];
				}
				else
				{
					// Add space and word
					$result .= " ".$words[$i];
				}
				
				$record[] = $words[$i];
				
			}
		}
		echo (" Input  : ".$text."\n");
		echo (" Result : ".$result."\n");
	}
}

function main()
{
	$task = new Finding();
	$text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
	$task->findRepeatedwords($text);
}
main();

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
/*
    Node JS program
    Remove duplicate words from string
*/
class Finding
{
	findRepeatedwords(text)
	{
		// Collect all words of string
		var words = text.split(" ");
		// Using of this to collect words and display status.
		var record = new Set();
		var n = words.length;
		var result = "";
		// Execute loop to check all words
		for (var i = 0; i < n; i++)
		{
			if (!record.has(words[i]))
			{
				if ((result.localeCompare("") == 0))
				{
					// Add word
					result += words[i];
				}
				else
				{
					// Add space and word
					result += " " + words[i];
				}
				record.add(words[i]);
			}
		}
		console.log(" Input  : " + text);
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new Finding();
	var text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
	task.findRepeatedwords(text);
}
main();

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
#    Python 3 program
#    Remove duplicate words from string
class Finding :
	def findRepeatedwords(self, text) :
		#  Collect all words of string
		words = text.split(" ")
		#  Using of this to collect words and display status.
		record = set()
		n = len(words)
		result = ""
		i = 0
		#  Execute loop to check all words
		while (i < n) :
			if (not words[i] in record) :
				if (result == "") :
					#  Add word
					result += words[i]
				else :
					#  Add space and word
					result += " " + words[i]
				
				record.add(words[i])
			
			i += 1
		
		print(" Input  : ", text)
		print(" Result : ", result)
	

def main() :
	task = Finding()
	text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple"
	task.findRepeatedwords(text)

if __name__ == "__main__": main()

input

 Input  :  banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result :  banana Bing Cherry Apple mango orange BingCherry apple
require 'set'
#    Ruby program
#    Remove duplicate words from string
class Finding 
	def findRepeatedwords(text) 
		#  Collect all words of string
		words = text.split(" ")
		#  Using of this to collect words and display status.
		record = SortedSet.new()
		n = words.length
		result = ""
		i = 0
		#  Execute loop to check all words
		while (i < n) 
			if (!record.include?(words[i])) 
				if (result == "") 
					#  Add word
					result += words[i]
				else
 
					#  Add space and word
					result += " "+ words[i]
				end

				record.add(words[i])
			end

			i += 1
		end

		print(" Input  : ", text, "\n")
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = Finding.new()
	text = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple"
	task.findRepeatedwords(text)
end

main()

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
import scala.collection.mutable._;
/*
    Scala program
    Remove duplicate words from string
*/
class Finding()
{
	def findRepeatedwords(text: String): Unit = {
		// Collect all words of string
		var words: Array[String] = text.split(" ");
		// Using of this to collect words and display status.
		var record = Set[String]();
		var n: Int = words.length;
		var result: String = "";
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (!record.contains(words(i)))
			{
				if (result.equals(""))
				{
					// Add word
					result += words(i);
				}
				else
				{
					// Add space and word
					result += " " + words(i);
				}
				record.add(words(i));
			}
			i += 1;
		}
		println(" Input  : " + text);
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Finding = new Finding();
		var text: String = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
		task.findRepeatedwords(text);
	}
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple
import Foundation;
/*
    Swift 4 program
    Remove duplicate words from string
*/
class Finding
{
	func findRepeatedwords(_ text: String)
	{
		// Collect all words of string
		let words: [String] = text.split
		{
			$0 == " "
		}.map(String.init);
		// Using of this to collect words and display status.
		var record = Set<String>();
		let n: Int = words.count;
		var result: String = "";
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (!record.contains(words[i]))
			{
				if (result == "")
				{
					// Add word
					result += words[i];
				}
				else
				{
					// Add space and word
					result += " "
					+ words[i];
				}
				record.insert(words[i]);
			}
			i += 1;
		}
		print(" Input  : ", text);
		print(" Result : ", result);
	}
}
func main()
{
	let task: Finding = Finding();
	let text: String = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
	task.findRepeatedwords(text);
}
main();

input

 Input  :  banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result :  banana Bing Cherry Apple mango orange BingCherry apple
/*
    Kotlin program
    Remove duplicate words from string
*/
class Finding
{
	fun findRepeatedwords(text: String): Unit
	{
		// Collect all words of string
		var words = text.split(" ");
		// Using of this to collect words and display status.
		val record = mutableSetOf <String> ();
		val n: Int = words.count();
		var result: String = "";
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (!record.contains(words[i]))
			{
				if (result.equals(""))
				{
					// Add word
					result += words[i];
				}
				else
				{
					// Add space and word
					result += " " + words[i];
				}
				record.add(words[i]);
			}
			i += 1;
		}
		println(" Input  : " + text);
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Finding = Finding();
	val text: String = "banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple";
	task.findRepeatedwords(text);
}

input

 Input  : banana Bing Cherry Apple mango Bing Cherry banana orange mango BingCherry Apple apple
 Result : banana Bing Cherry Apple mango orange BingCherry apple




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