Given a list, find the longest non-repeating (in other words I mean unique) string in the input list. Looking for code review, pointers on best practices, optimizations etc.
public final class LongestUniqueWord {
/*
* do not initialize this class
*/
private LongestUniqueWord( ) {}
/**
* Returns the longest non-repeating(unique) word in the list.
* If each word repeats then return null.
*
*
* @param words the list of words of a news paper.
* @return the longest unique word.
* @throws IllegalArgumentException if input size is 0.
* @throws NPE if the input is null.
*/
public static String longestUniqueWord (List<String> words) {
if (words.size() == 0) throw new IllegalArgumentException("The array should not be empty.");
final Map<String, Boolean> uniqueWords = new HashMap<String, Boolean>();
for (String word : words) {
if (uniqueWords.containsKey(word)) {
uniqueWords.put(word, false);
} else {
uniqueWords.put(word, true);
}
}
int maxLength = 0;
String longestUniqueWord = null;
for (Entry<String, Boolean> entry : uniqueWords.entrySet()) {
if (entry.getValue() && entry.getKey().length() > maxLength) {
longestUniqueWord = entry.getKey();
maxLength = longestUniqueWord.length();
}
}
return longestUniqueWord;
}
public static void main(String[] args) {
List<String> newsPaper = new ArrayList<String>();
newsPaper.add("Jack");
newsPaper.add("In");
newsPaper.add("In");
newsPaper.add("The");
newsPaper.add("The");
newsPaper.add("Box");
System.out.println("Expected: Jack, Actual: " + longestUniqueWord(newsPaper));
newsPaper.add("Jack");
System.out.println("Expected: Box, Actual: " + longestUniqueWord(newsPaper));
}
}