java在两个数组中查找匹配的数字
嘿,我想写一个方法,比较两个数组,并返回它们共有的值的数量
例如,如果有两个数组:
arr{0,4,2,5}
arr1{0,7,4,4}
然后该方法将返回2
这就是我目前的情况:
public int numDigitsCorrect(Permutation other) {
int c=0;
for(int i=0; i<nums.length; i++) {
for(int x=0; x<nums.length;x++) {
if(nums[i]==other.nums[x]) {
System.out.println(nums[i] + " " + other.nums[x] + " ");
c++;
}
}
}
return c;
}
# 1 楼答案
如果你使用Set,它会更加清晰易读。这也将降低代码的复杂性
比如:
输出: