如何避免数组重复检查中已验证值的重复输出?
问题描述
我用Utils.arrGen生成了两个指定长度、取值范围的随机数组firstArr和secondArr,用Utils类的arrRepeatCheck函数检查firstArr元素在secondArr中的出现次数并遍历输出结果,但同一值会重复打印(示例输出如下)。我试过用if判断值是否已检查但没成功,怎么解决?
原代码实现
数组生成代码
int [] firstArr = Utils.arrGen(len, min, max); int [] secondArr = Utils.arrGen(len, min, max);
遍历检查代码
for (int i = 0; i < firstArr.length; i++) { int [] result = Utils.arrRepeatCheck(secondArr, firstArr[i]); if (result[1] != 0) { System.out.println("The number " + result[0] + " was found to be repetetive in the arrays " + result[1] + " times!"); } }
arrRepeatCheck函数实现
public static int [] arrRepeatCheck(int [] arr, int num) { int counter = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == num) { counter++; } } int [] result = {num, counter}; return result; }
注:
Utils.arrGen函数用于生成指定取值范围内的随机值数组,arrRepeatCheck函数已在Utils类中实现。
当前输出结果
The number 1 was found to be repetetive in the arrays 12 times! The number 3 was found to be repetetive in the arrays 15 times! The number 1 was found to be repetetive in the arrays 12 times! The number 1 was found to be repetetive in the arrays 12 times! The number 2 was found to be repetetive in the arrays 8 times! The number 0 was found to be repetetive in the arrays 15 times! The number 1 was found to be repetetive in the arrays 12 times! The number 0 was found to be repetetive in the arrays 15 times! The number 2 was found to be repetetive in the arrays 8 times! The number 0 was found to be repetetive in the arrays 15 times! The number 2 was found to be repetetive in the arrays 8 times! The number 1 was found to be repetetive in the arrays 12 times! The number 3 was found to be repetetive in the arrays 15 times! The number 2 was found to be repetetive in the arrays 8 times! The number 3 was found to be repetetive in the arrays 15 times! The number 3 was found to be repetetive in the arrays 15 times! The number 1 was found to be repetetive in the arrays 12 times! The number 3 was found to be repetetive in the arrays 15 times! The number 0 was found to be repetetive in the arrays 15 times! The number 3 was found to be repetetive in the arrays 15 times! The number 3 was found to be repetetive in the arrays 15 times! The number 0 was found to be repetetive in the arrays 15 times! The number 3 was found to be repetetive in the arrays 15 times! The number 2 was found to be repetetive in the arrays 8 times! The number 1 was found to be repetetive in the arrays 12 times! The number 3 was found to be repetetive in the arrays 15 times! The number 0 was found to be repetetive in the arrays 15 times! The number 3 was found to be repetetive in the arrays 15 times! Process finished with exit code 0
解决方案
核心思路是记录已经检查过的元素,避免重复处理,以下是几种可行的实现方式:
方法1:用Set存储已检查的元素
遍历firstArr时,先判断当前元素是否已经在Set中,不在的话才执行检查并输出,同时把元素加入Set:
// 新增一个Set来记录已检查过的数字 Set<Integer> checkedNumbers = new HashSet<>(); for (int i = 0; i < firstArr.length; i++) { int currentNum = firstArr[i]; // 如果该数字没检查过 if (!checkedNumbers.contains(currentNum)) { int[] result = Utils.arrRepeatCheck(secondArr, currentNum); if (result[1] != 0) { System.out.println("数字 " + result[0] + " 在数组中出现了 " + result[1] + " 次!"); } // 标记为已检查 checkedNumbers.add(currentNum); } }
方法2:先对firstArr去重,再遍历检查
先把firstArr转换成不含重复元素的集合,再遍历这个集合执行检查,从根源避免重复处理:
// 对firstArr去重 Set<Integer> uniqueFirstArr = new HashSet<>(); for (int num : firstArr) { uniqueFirstArr.add(num); } // 遍历去重后的集合 for (int num : uniqueFirstArr) { int[] result = Utils.arrRepeatCheck(secondArr, num); if (result[1] != 0) { System.out.println("数字 " + result[0] + " 在数组中出现了 " + result[1] + " 次!"); } }
方法3:优化统计逻辑,一次遍历完成统计
如果不想额外用集合,也可以先统计secondArr中所有元素的出现次数,再遍历firstArr的唯一元素输出,效率更高(避免多次调用arrRepeatCheck重复遍历secondArr):
// 先统计secondArr中每个数字的出现次数 Map<Integer, Integer> countMap = new HashMap<>(); for (int num : secondArr) { countMap.put(num, countMap.getOrDefault(num, 0) + 1); } // 再遍历firstArr的唯一元素输出 Set<Integer> uniqueFirstArr = new HashSet<>(); for (int num : firstArr) { if (!uniqueFirstArr.contains(num)) { int count = countMap.getOrDefault(num, 0); if (count != 0) { System.out.println("数字 " + num + " 在数组中出现了 " + count + " 次!"); } uniqueFirstArr.add(num); } }
这些方法都能解决重复输出的问题,其中方法3的效率最高,因为只需要遍历secondArr一次,而原代码中每个firstArr元素都会遍历一次secondArr,数据量大时差异明显。
内容的提问来源于stack exchange,提问作者shitposter
相关产品推荐
相关产品推荐

