用Hash表存放String的个数,先遍历一遍全部的String,记录个数
再遍历一遍Hash表找到个数是1的就是唯一的单词
class Solution { public String[] uncommonFromSentences(String A, String B) { List<String> list = new ArrayList<>(); String[] split = A.split(" "); String[] split2 =B.split(" "); HashMap<String,Integer> map = new HashMap<>(); for(String s:split) { map.put(s, map.getOrDefault(s, 0)+1); } for(String s2:split2) { map.put(s2,map.getOrDefault(s2, 0)+1); } for(Map.Entry<String, Integer> entry :map.entrySet()) { if(entry.getValue() == 1) { list.add(entry.getKey()); } } return list.toArray(new String[list.size()]); } }
原文:https://www.cnblogs.com/cocobear9/p/12945130.html