Given a list of Strings, return a list that contains the distinct values. In other words, no value is to be included more than once in the returned list. (Hint: Think Set)
distinctValues( ["red", "yellow", "green", "yellow", "blue", "green", "purple"] ) -> ["red", "yellow", "green", "blue", "purple"]
distinctValues( ["jingle", "bells", "jingle", "bells", "jingle", "all", "the", "way"] ) -> ["jingle", "bells", "all", "the", "way"]
My Code:
public List<String> distinctValues(List<String> stringList) {
Map<String, String> distinctValuesMap = new HashMap<String, String>();
for (int i= 0; i < stringList.size(); i++) {
distinctValuesMap.put(stringList.get(i), stringList.get(i));
}
Set<String>distinctValuesSet = distinctValuesMap.keySet();
List<String>distinctValues = new ArrayList<String>(distinctValuesSet);
System.out.println(distinctValues);
return distinctValues;
}
Expected results: red, yellow, green, blue, purple My results: red, green, blue, yellow, purple
Not sure where I'm going wrong, but I tried different ways and can't get the expect outcome.