List
of String
s, write a method removeDuplicates
that removes duplicate words from the List
and returns an ArrayList
of all the unique words. The returned ArrayList
should be lexically alphabetically.Input: [Hi, Hello, Hey, Hi, Hello, Hey]
Output: [Hello, Hey, Hi]
TreeSet
101. TreeSet
s not only hold only unique values but also maintain the natural order of the elements contained within. Since the output ArrayList
needs to be lexically sorted, let us use a TreeSet
to maintain the natural sorting order of inserted Strings.
TreeSet
to an ArrayList
like so - ArrayList newList
= new ArrayList(treeSet);
TreeSet
called treeSet
.String
s from the input list to treeSet
.new ArrayList(treeSet);
.
public static ArrayList<String> removeDuplicates(List<String> input) { }
C
Java
Python