Name Deduplication(lintcode 487)

Description

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.

Example

Given:

["James", "james", "Bill Gates", "bill Gates", "Hello World", "HELLO WORLD", "Helloworld"]

return:

["james", "bill gates", "hello world", "helloworld"]

Interface

public class Solution {
    /**
     * @param names a string array
     * @return a string array
     */
    public List<String> nameDeduplication(String[] names) {
        // Write your code here
    }
}

Solution

public class Solution {
    /**
     * @param names a string array
     * @return a string array
     */
    public List<String> nameDeduplication(String[] names) {
        // Write your code here
        HashSet<String> uniqueNames = new HashSet<String>();
        List<String> result = new ArrayList<String>();

        for (int i = 0; i < names.length; ++i) {
            if (!uniqueNames.contains(names[i].toLowerCase())) {
                uniqueNames.add(names[i].toLowerCase());
                result.add(names[i].toLowerCase());
            }
        }

        return result;
    }
}

results matching ""

    No results matching ""