Name Duplication

Question:

Given a list of names, remove the duplicate names. Two name will be treated as the same name if they are equal ignore the case.

Return a list of names without duplication, all names should be in lowercase, and keep the order in the original list.

Analysis:

用hashset解决duplicate的问题

HashSet

Set<String> set= new Set<String>();

set.add(names[i]);

set.contains(names[i])

Code

public class Solution {
    /**
     * @param names a string array
     * @return a string array
     */
    public List<String> nameDeduplication(String[] names) {
        // Write your code here
        Set<String> set = new HashSet<String>();
        List<String> lists = new ArrayList<String>();
        int count = 0;
        for (int i = 0; i < names.length; i++) {
            names[i] = names[i].toLowerCase();
            if (set.contains(names[i])) {
                continue;
            } else {
                set.add(names[i]);
                lists.add(names[i]);
            }
        }
        return lists;
    }
}