487. 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.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Example

Example 1:

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


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

Clarification

You can assume that the name contains only uppercase and lowercase letters and spaces.

思路:

用hashmap去除重复,先把字符串转小写,调用toLowerCase方法,返回值要重新赋值给str!! 

代码:

 1 public class Solution {
 2     /**
 3      * @param names: a string array
 4      * @return: a string array
 5      */
 6     public List<String> nameDeduplication(String[] names) {
 7         List<String> result = new ArrayList<String>();
 8         Map<String, Integer> map = new HashMap<String, Integer>();
 9         for (String str : names) {
10             str = str.toLowerCase();
11             if (!map.containsKey(str)) {
12                 map.put(str, 1);
13                 result.add(str);
14             }
15         }
16         return result;
17     }
18 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄