Reputation: 1
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
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
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
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