Print all the non-repeating words from the two given sentences


In this tutorial, we will identify and print all non-repeating words from two given sentences. Non-repeating words refer to words that appear only once in both sentences, means they do not repeat in other sentence. The task involves analysis of the input sentences, identification of the individual words, and comparing them across both sentences to find which ones appear only once. The output should be a list of all such words. This task can be accomplished through various programming approaches, such as using loops, arrays, or dictionaries.

Methods

Here are the two methods to print all the non-repeating words from two given sentences −

Method 1: Using Dictionary

Method 2: Using Sets

Method 1: Using Dictionary

Using dictionary, count number of times each word appears in two phrases. Then we can go through dictionary and print all words that appear only once. The Dictionary function in C++ is commonly used to output all non-repeating words from two specified sentences. This method includes storing frequency of each word in both phrases using dictionary or hash table data structure. Then we may go through dictionary iteratively and print out terms that appear only once.

Syntax

Here is the syntax without actual code to print all the non-repeating words from two given sentences using a dictionary method in C++ −

  • Declare a dictionary to store word frequency

map<string, int> freqDict;
  • Input two sentences as strings

string sentence1 = "first sentence";
string sentence2 = "second sentence";
  • Split the sentences into words and insert them into the dictionary

istringstream iss (sentence1 + " " + sentence2);
string word;
while (iss >> word) {
   freqDict[word]++;
}
  • Iterate through the dictionary and print the non-repeating words

for (const auto& [word, frequency]: freqDict) {
   if (frequency == 1) {
      cout << word << " ";
   }
}

Algorithm

In C++, here is step-by-step technique for using Dictionary method to print all non-repeating terms from two specified sentences −

Step 1 − Create two strings, s1 and s2, which contain sentences.

Step 2 − Declare empty unordered map string, int> dict to record frequency of each word in sentences.

Step 3 − Using C++'s string stream class, parse both phrases to extract individual words.

Step 4 − For each extracted word, check to see if it appears in dict. If it is, increase its frequency by one. Otherwise, add it to dict with frequency of 1.

Step 5 − After processing both sentences, iterate through dict and display all terms having frequency of one. These are non-repeating words from the two sentences.

Step 6 − The temporal complexity of this approach is O(n),

Example 1

This code used an unordered map to store frequency of each word in combined phrase. Then, it loops across the map, adding each word that appears just once to a vector of non-repeating words. Finally, it publishes non-repeating words. This example implies that two sentences are hardcoded into programme rather than entered by user.

#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>
#include <vector>

using namespace std;

vector<string> getNonRepeatingWords(string sentence1, string sentence2) {
   // Combine the two sentences into a single string
   string combined = sentence1 + " " + sentence2;

   // Create a map to store the frequency of each word
   unordered_map<string, int> wordFreq;

   // Use a string stream to extract each word from the combined string
   stringstream ss(combined);
   string word;
   while (ss >> word) {
      // Increment the frequency of the word in the map
      wordFreq[word]++;
   }

   // Create a vector to store the non-repeating words
   vector<string> nonRepeatingWords;
   for (auto& pair : wordFreq) {
      if (pair.second == 1) {
         nonRepeatingWords.push_back(pair.first);
      }
   }

   return nonRepeatingWords;
}
int main() {
   string sentence1 = "The quick brown fox jumps over the lazy dog";
   string sentence2 = "A quick brown dog jumps over a lazy fox";

   vector<string> nonRepeatingWords = getNonRepeatingWords(sentence1, sentence2);

   // Print the non-repeating words
   for (auto& word : nonRepeatingWords) {
      cout << word << " ";
   }
   cout << endl;

   return 0;
}

Output

a A the The

Method 2: Using Sets

This strategy includes using sets to locate terms that appear just once in both phrases. We can build sets of terms for each phrase and then identify the intersection of these sets. Finally, we can iterate through intersection set and output all the terms that appear only once.

A set is associative container that keeps distinct elements in sorted order. We can insert terms from two phrases into the set, and any duplicates will be deleted automatically.

Syntax

Sure! Here's the syntax you could use in Python to print all non-repeating words from two given sentences −

  • Define the two sentences as strings

sentence1 = "The fox jumps over dog"
sentence2 = "A dog jumps over fox"
  • Split each sentence into a list of words

words1 = sentence1.split()
words2 = sentence2.split()
  • Create sets from the two lists of words

set1 = set(words1)
set2 = set(words2)
  • Find non-repeating words by taking intersection of sets

Nonrepeating = set1.symmetric_difference(set2)
  • Print non-repeating words

for word in non-repeating:
   print(word)

Algorithm

Follow these instructions to output all non-repeating words from two given sentences using sets function in C++ −

Step 1 − Create two string variables to store two sentences.

Step 2 − Using string stream library, split each sentence into independent words and store them in two separate arrays.

Step 3 − Make two sets, one for each sentence, to store unique words.

Step 4 − Loop through each array of words and insert each word into correct collection.

Step 5 − Go through each set and print out non-repeating words.

Example 2

In this code, we use string stream library to split each sentence into separate words. Then we use two sets, uniqueWords1 and uniqueWords2, to store unique words from each sentence. Finally, we loop through each set and print out the non-repeating words.

#include <iostream>
#include <string>
#include <sstream>
#include <set>

using namespace std;

int main() {
   string sentence1 = "This is the first sentence.";
   string sentence2 = "This is the second sentence.";
   string word;
   stringstream ss1(sentence1);
   stringstream ss2(sentence2);
   set<string> uniqueWords1;
   set<string> uniqueWords2;

   while (ss1 >> word) {
      uniqueWords1.insert(word);
   }

   while (ss2 >> word) {
      uniqueWords2.insert(word);
   }

   cout << "Non-repeating words in sentence 1:" << endl;
   for (const auto& w : uniqueWords1) {
      if (uniqueWords2.find(w) == uniqueWords2.end()) {
         cout << w << " ";
      }
   }
   cout << endl;

   cout << "Non-repeating words in sentence 2:" << endl;
   for (const auto& w : uniqueWords2) {
      if (uniqueWords1.find(w) == uniqueWords1.end()) {
         cout << w << " ";
      }
   }
   cout << endl;

   return 0;
}

Output

Non-repeating words in sentence 1:
first 
Non-repeating words in sentence 2:
second

Conclusion

In conclusion, task of printing all non-repeating words from two provided sentences is achieved using various programming methods, like breaking sentences up into individual words, utilizing dictionaries to quantify frequency of each word, and filtering out non-repeating words. The resulting collection of non-repeating words can either be reported to the console or saved in a list or array for further use. This work is helpful practice for basic programming text manipulation and data structure operations.

Updated on: 01-Aug-2023

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements