You can fill your words in a map witch has a key the word and the value the number of occurrence.
When you finish filling your data in your Map loop again again check the value if great then on or equal one, if it is great then one, remove it from your map.
And you can return the size of this map in the end, to know the number of unique words
If you dont want to use just abc
or ABC
, you ca use LowerCase
or UpperCase
when you insert in your Map.
Here a simple example you can go throw :
public static void main(String[] args) {
//i consider you know how to learn from a file, i use a simple array just to explain
String[] listWords = {"hello", "word", "hello", "stack", "HELLO", "WORD", "123", "what?"};
//fill your words in your map
Map<String, Integer> map = new HashMap<>();
for (String word : listWords) {
if (!map.containsKey(word.toLowerCase())) {
map.put(word.toLowerCase(), 1);
} else {
map.put(word.toLowerCase(), map.get(word.toLowerCase()) + 1);
}
}
//print your map
map.entrySet().forEach((entry) -> {
System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
});
System.out.println("**************************************************");
//loop throw your map and remove the words which occurrence > 1
for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Integer> entry = it.next();
if (entry.getValue() > 1) {
it.remove();
}
}
//print your map again
map.entrySet().forEach((entry) -> {
System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
});
//size of your end map
System.out.println("Size of map = " + map.size());
}
Some good references you can base one to play with maps:
How to update a value, given a key in a java hashmap?
iterating over and removing from a map
Hope this can gives you an idea.