/** * 找出两个数组相同的元素,并且对应的个数一样 * @param args */ public static void getSameNumberCount(String[] a, String[] b) { Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < a.length; i++) { if (!map.containsKey(a[i])) { map.put(a[i], 1); } else { map.put(a[i], map.get(a[i]) + 1); } } for (int j = 0; j < b.length; j++) { if (map.containsKey(b[j])) { System.out.println(b[j]); if (map.get(b[j]) > 1) { map.put(b[j], map.get(b[j]) - 1); } else { map.remove(b[j]); } } } } public static void main(String[] args) { String[] x = {"a","b","c","d"}; String[] y = {"a","a","b","b","c"}; getSameNumberCount(y, x); }
时间: 2024-11-03 13:33:21