求一个和list 去重很像的算法实现 将一个list中 重复的数据 全部删掉, 是全部删掉, 只要重复了, 就删掉包括自己 如:
* ["aa","aa","bb","dd","ee","ee"] 处理后返回 ["bb","dd"]
public class Check {
public static void main(String[] args) {
List<String> list = Arrays.asList(new String[]{"aa", "aa", "bb", "dd", "ee", "ee"});
System.out.println(check(list));
}
private static List<String> check(List<String> list) {
Map<String, Integer> map = new HashMap<>();
for (String s : list) {
if (map.get(s) == null) {
map.put(s, 1);
} else {
map.put(s, map.get(s) + 1);
}
}
List<String> resultList = new ArrayList<>();
for (String key : map.keySet()) {
if (map.get(key) == 1) {
resultList.add(key);
}
}
return resultList;
}
}
添加到hashset中,以字符串作为key,数量作为value。
最后循环输出value=1的
就用set源代码里的去重,set里面是重复就不添加,改为重复就remove掉这条数据,不就好了