Name Deduplication

题目:

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.

分析:

注意HashSet用contains(),HashMap用containsKey()

解法:

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

results matching ""

    No results matching ""