TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find the Duplicate Words in a String

Program to Find the Duplicate Words in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to find the duplicate words in a string

Explanation

In this program, we need to find out the duplicate words present in the string and display those words.

big black bug bit a big black dog on his big black nose

To find the duplicate words from the string, we first split the string into words. We count the occurrence of each word in the string. If count is greater than 1, it implies that a word has duplicate in the string.

In above example, the words highlighted in green are duplicate words.

Algorithm

  1. Define a string.
  2. Convert the string into lowercase to make the comparison insensitive.
  3. Split the string into words.
  4. Two loops will be used to find duplicate words. Outer loop will select a word and Initialize variable count to 1. Inner loop will compare the word selected by outer loop with rest of the words.
  5. If a match found, then increment the count by 1 and set the duplicates of word to '0' to avoid counting it again.
  6. After the inner loop, if count of a word is greater than 1 which signifies that the word has duplicates in the string.

Solution

Python

string = "big black bug bit a big black dog on his big black nose";
 
#Converts the string into lowercase
string = string.lower();
 
#Split the string into words using built-in function
words = string.split(" ");
 
print("Duplicate words in a given string : ");
for i in range(0, len(words)):
    count = 1;
    for j in range(i+1, len(words)):
        if(words[i] == (words[j])):
            count = count + 1;
            #Set words[j] to 0 to avoid printing visited word
            words[j] = "0";
            
    #Displays the duplicate word if count is greater than 1
    if(count > 1 and words[i] != "0"):
        print(words[i]);

Output:

 Duplicate words in a given string : 
big
black

C

#include <stdio.h>
#include <string.h>
 
int main()
{   
    char string[] = "big black bug bit a big black dog on his big black nose";
    char words[100][100];
    int i = 0, j = 0, k, length, count;
    
    //Split the string into words such that each row of array words represents a word
    for(k=0; string[k]!='\0'; k++){
        
        //Here, i represents row and j represents column of two-dimensional array words
        if(string[k] != ' ' && string[k] != '\0'){
            //Converts the string into lowercase and add it to array words
            words[i][j++] = tolower(string[k]);
        }
        else{
            words[i][j] = '\0';
            //Increment row count to store new word
            i++;
            //Set column count to 0
            j = 0;
        }
    }
    
    //Store row count in variable length
    length = i+1;
    
    printf("Duplicate words in the given string: \n");
    for(i = 0; i < length; i++){
        count = 1;
        for(j = i+1; j < length; j++){
           if(strcmp(words[i], words[j]) == 0 && (strcmp(words[j],"0") != 0)){
               count++;
               //Set words[j] to 0 to avoid printing visited word
               strcpy(words[j],"0");
           } 
        }
        //Displays the duplicate word if count is greater than 1
        if(count > 1 )
            printf("%s\n", words[i]);
    }
  
    return 0;
}

Output:

Duplicate words in a given string : 
big
black

JAVA

public class DuplicateWord {
    public static void main(String[] args) {
        String string = "Big black bug bit a big black dog on his big black nose";
        int count;
        
        //Converts the string into lowercase
        string = string.toLowerCase();
        
        //Split the string into words using built-in function
        String words[] = string.split(" ");
        
        System.out.println("Duplicate words in a given string : "); 
        for(int i = 0; i < words.length; i++) {
            count = 1;
            for(int j = i+1; j < words.length; j++) {
                if(words[i].equals(words[j])) {
                    count++;
                    //Set words[j] to 0 to avoid printing visited word
                    words[j] = "0";
                }
            }
            
            //Displays the duplicate word if count is greater than 1
            if(count > 1 && words[i] != "0")
                System.out.println(words[i]);
        }
    }
}

Output:

Duplicate words in a given string : 
big
black

C#

 using System;
                    
public class DuplicateWord
{
    public static void Main()
    {
        String string1 = "Big black bug bit a big black dog on his big black nose";
        int count;
        
        //Converts the string into lowercase
        string1 = string1.ToLower();
        
        //Split the string into words using built-in function
        String[] words = string1.Split(' ');
        
        Console.WriteLine("Duplicate words in a given string : ");
        for(int i = 0; i < words.Length; i++) {
            count = 1;
            for(int j = i+1; j < words.Length; j++) {
                if(words[i].Equals(words[j])) {
                    count++;
                    //Set words[j] to 0 to avoid printing visited word
                    words[j] = "0";
                }
            }
            
            //Displays the duplicate word if count is greater than 1
            if(count > 1 && words[i] != "0")
                Console.WriteLine(words[i]);
        }
    }
}

Output:

Duplicate words in a given string : 
big
Black

PHP

<!DOCTYPE html>
<html>
<body>
<?php
$string = "Big black bug bit a big black dog on his big black nose";
$count;
 
//Converts the string into lowercase
$string = strtolower($string);
 
//Split the string into words using built-in function
$words = explode(" ", $string);
 
print("Duplicate words in a given string : <br>");
for($i = 0; $i < count($words); $i++) {
    $count = 1;
    for($j = $i+1; $j < count($words); $j++) {
        if($words[$i] == $words[$j]) {
            $count++;
            //Set words[j] to 0 to avoid printing visited word
            $words[$j] = "0";
        }
    }
    
    //Displays the duplicate word if count is greater than 1
    if($count > 1 && $words[$i] != "0"){
        print($words[$i]);
        print("<br>");
    }
}
?>
</body>
</html>

Output:

Duplicate words in a given string : 
big
black

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf