user1080383
user1080383

Reputation: 1

Finding Distinct Words between 2 sentences in java

What is an efficient way to find out all the unique words between 2 sentences in java and store them? What data structure should be used to store the words?

Upvotes: 0

Views: 487

Answers (3)

Aba Dov
Aba Dov

Reputation: 962

A simple way of achieving this is:

//I   use regular expression to remove punctuation marks
//II  use split to convert the sentences into collections of "words"
//III create a variable that is an implementation of java.util.set (to store unique words)
//III iterate over the collections 
//    add words from each sentence to the set variable (that way the word will only be stored once)

Hope this helps

Upvotes: 0

Saurabh
Saurabh

Reputation: 7964

Store words from the first sentence in hashset and then iterate over ords in second sentence to see if its already there in hashset

Upvotes: 1

ipavlic
ipavlic

Reputation: 4966

Put all words from one sentence in a set, then pass through words of the second sentence. If the word exists in a set, take it out of the set, otherwise put it into the set.

Upvotes: 0

Related Questions