Problem : Find the first unique word in entire book .
Solution:
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.stream.Stream;
public class UniqueWordInBook {
static String [] words = {"find","the","first","unique","word","in","entire","book"};
public static void main(String[] args) {
LinkedHashMap
Random rand = new Random();
Stream
stringStream.forEach(str -> {
System.out.println(str);
if(wordsVisited.containsKey(str)) {
wordsVisited.remove(str);
wordsVisited.put(str, true);
} else {
wordsVisited.put(str, false);
}
});
Entry
if(!uniqEntry.getValue()) {
System.out.println("First unique word : " + uniqEntry.getKey());
} else {
System.out.println("Not present");
}
}
}
This solution can be used to find the first unique char present in a String.