-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWords.java
More file actions
20 lines (18 loc) · 752 Bytes
/
Words.java
File metadata and controls
20 lines (18 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package words;
import java.util.List;
import java.util.ArrayList;
public class Words {
public static List<String> getUniqueWordsFromSentence(String sentence) {
// Remove all the punctuation in the string and split the string into a list of strings
// by splitting the sentence with every space
String[] words = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
List<String> indi_words = new ArrayList<>();
for(int i = 0; i < words.length; i++) {
// Checking if the word in the sentence is already in the individual words list
if(indi_words.contains(words[i]) == false) {
indi_words.add(words[i]);
}
}
return indi_words;
}
}