Java在整数数组中重复 [英] Java duplicates in integer array
本文介绍了Java在整数数组中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要查找 int []
数组是否包含重复项。我不能使用收藏品。
I need to find if an int[]
array contains duplicates. I can not use collections.
我的解决方案(不起作用)是:
My solution (which does not work) is:
boolean containsDuplicates(int[]list1, int[]list2) {
if (list1.length != list2.length) {
return false;
}
for (int i = 0; i < list1.length; i++) {
if (list1[i] != list2[i]) {
return false;
}
}
return true;
}
推荐答案
解决方案,不是最好的在计算周期但没有内存开销:
A solution, not the best in computation cycles but without memory overhead:
- 一个循环,用于将数组A从0迭代到N,索引为M
- 一个嵌套循环,用于将A从M + 1迭代到N,索引为S
- if(A [M] = A [S])返回true
- 返回false(未找到重复项)
- A loop to iterate the array A from 0 to N with index M
- A nested loop to iterate A from M+1 to N with index S
- if( A[M]=A[S] ) return true
- return false (no duplicates found)
这篇关于Java在整数数组中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文