英文:
Finding duplicate numbers in two arrays
问题
import java.security.SecureRandom;
public class Duplicates
{
public static void main(String []args)
{
int[] array1 = new int[10];
int[] array2 = new int[10];
SecureRandom randomNumbers = new SecureRandom();
for(int i = 0; i < array1.length; i++)
{
array1[i] = randomNumbers.nextInt(19);
}
for(int i = 0; i < array2.length; i++)
{
array2[i] = randomNumbers.nextInt(19);
}
System.out.printf("Array 1: ");
for(int i: array1)
{
System.out.print(i + ",");
}
System.out.println();
System.out.printf("Array 2: ");
for(int i: array2)
{
System.out.print(i + ",");
}
System.out.println("\nValues that exist in both arrays are... ");
for(int i = 0; i < array1.length;i++)
{
for(int j = 0; j < array2.length; j++)
{
if(array1[i]==array2[j])
{
System.out.printf(array1[i] + ",");
break; // Add this line to break the inner loop after finding a duplicate
}
}
}
}
}
英文:
I have been asked to make a program that can find duplicate numbers within two arrays. I have figured out how to get the duplicate numbers to print out but I am having a problem getting them to print out with only one number of the duplicated. ie;
int[] array1 = new int[2,4,6,8,10,12,14,16,18,20]
int[] array1 = new int[2,4,6,8,10,10,6,7,20,20]
output:
2, 4, 6, 8, 10, 10, 6, 20, 20
How would I get it to only output one 20
and one 10
?
import java.security.SecureRandom;
public class Duplicates
{
public static void main(String []args)
{
int[] array1 = new int[10];
int[] array2 = new int[10];
SecureRandom randomNumbers = new SecureRandom();
for(int i = 0; i < array1.length; i++)
{
array1[i] = randomNumbers.nextInt(19);
}
for(int i = 0; i < array2.length; i++)
{
array2[i] = randomNumbers.nextInt(19);
}
System.out.printf("Array 1: ");
for(int i: array1)
{
System.out.print(i + ",");
}
System.out.println();
System.out.printf("Array 2: ");
for(int i: array2)
{
System.out.print(i + ",");
}
System.out.println("\nValues the exist in both arrays are... ");
for(int i = 0; i < array1.length;i++)
{
for(int j = 0; j < array2.length; j++)
{
if(array1[i]==array2[j])
{
System.out.printf(array1[i] + ",");
}
}
}
}
}
答案1
得分: 3
你需要一种记录已经被识别为重复的数字的方法。
我建议:创建一个新的集合,例如 HashSet,并将重复的值添加到其中,而不是打印它们。一旦你遍历了原始数组中的所有值,就打印重复集合中的内容。
英文:
You need a way of logging which numbers have already been identified as duplicates.
I suggust: make a new collection, e.g. a HashSet, and add duplicate values to that instead of printing them. Once you have gone through all values in the original arrays, print whatever is in the duplicates collection.
答案2
得分: 2
你可以创建一个名为 duplicates
的列表,并将所有重复值添加到该列表中。但在添加之前,请检查该列表是否已经包含了该值。
public class Main
{
public static void main(String[] args)
{
int[] array1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int[] array2 = {2, 4, 6, 8, 10, 10, 6, 7, 20, 20};
System.out.println("Array 1: " + java.util.Arrays.toString(array1)); // 更快的打印数组方式
System.out.println("Array 2: " + java.util.Arrays.toString(array2));
List<Integer> duplicates = new ArrayList<>(); // 用于存储所有重复值的列表
for (int k : array1)
{
if (duplicates.contains(k)) // 检查是否已经将此值添加到 duplicates 列表中
{
break;
}
for (int i : array2)
{
if (k == i)
{
duplicates.add(k);
break;
}
}
}
System.out.println("Duplicates: " + duplicates); // [2, 4, 6, 8, 10, 20]
}
}
我不确定,但我认为这可能不是在两个列表中搜索重复项的最有效方法。也许先对两个数组进行排序,然后再搜索重复项会更好。在更大的数据集中,你的两个嵌套循环会运行很长时间。(但这只是我的看法,我不确定)
英文:
You can create a list named duplicates
, and to that list add all duplicates values. But before adding check if that list already contains that value.
public class Main
{
public static void main(String[] args)
{
int[] array1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int[] array2 = {2, 4, 6, 8, 10, 10, 6, 7, 20, 20};
System.out.println("Array 1: " + java.util.Arrays.toString(array1)); // faster way to print array
System.out.println("Array 2: " + java.util.Arrays.toString(array2));
List<Integer> duplicates = new ArrayList<>(); // list for all duplicates
for (int k : array1)
{
if (duplicates.contains(k)) // check if You have already added this value to duplicates
{
break;
}
for (int i : array2)
{
if (k == i)
{
duplicates.add(k);
break;
}
}
}
System.out.println("Duplicates: " + duplicates); // [2, 4, 6, 8, 10, 20]
}
}
<hr>
I am not sure but I think this is not the most efficient way to search duplicates in two lists. Maybe sorting both arrays and then searching duplicates would be better. Your two nested loops will run very long in bigger data sets. (but it's just my opinion, I'm not sure)
答案3
得分: 2
以下是翻译的内容:
这里还有另一种方法。只需将这两个数组转换为 Set<Integer>
,然后使用 Set#retainAll。我使用了更多样化的数组来进行演示。
Integer[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
Integer[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };
Set<Integer> set1 = new HashSet<>(List.of(array1));
Set<Integer> set2 = new HashSet<>(List.of(array2));
set2.retainAll(set1);
System.out.println(set2);
输出
[2, 5, 7, 10]
如果你想使用原始数组,只需创建集合并复制值进去。
这里还有一种更传统的做法。
int[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
int[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };
创建一个源数组集合。
Set<Integer> set = new HashSet<>();
for (int i : array1) {
set.add(i);
}
创建一个空集合以保存重复项。
Set<Integer> dups = new HashSet<>();
现在遍历另一个数组。如果在之前创建的集合中找到一个元素,
将其添加到重复项的集合中。
for(int i : array2) {
if (set.contains(i)) {
dups.add(i);
}
}
System.out.println(dups);
输出
[2, 5, 7, 10]
在这里使用集合有两个优势。
- 对于集合,
contains
方法是高效的,因为它只需要检查与要查找对象的哈希码相关联的存储桶。 - 在累积重复项时,多个重复项只会被记录一次。
英文:
Here is another way. Just convert the two arrays to Set<Integer>
and use Set#retainAll. I am using more diverse arrays to demonstrate.
Integer[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
Integer[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };
Set<Integer> set1 = new HashSet<>(List.of(array1));
Set<Integer> set2 = new HashSet<>(List.of(array2));
set2.retainAll(set1);
System.out.println(set2);
Prints
[2, 5, 7, 10]
If you want to use primitive arrays, you can just create the sets and copy in the values.
And here is a more traditional way of doing it.
int[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
int[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };
Create a set of one of the source arrays.
Set<Integer> set = new HashSet<>();
for (int i : array1) {
set.add(i);
}
Create an empty set to hold the duplicates
Set<Integer> dups = new HashSet<>();
Now iterate thru the other array. If an element is found
in the previously created set, add it to the set of duplicates
for(int i : array2) {
if (set.contains(i)) {
dups.add(i);
}
}
System.out.println(dups);
Prints
[2, 5, 7, 10]
Using sets here has two advantages.
contains
for sets is efficient since it only needs to check the bucket associated with the hashcode of the object one is trying to find.- when accumulating duplicates, multiple duplicates will only be recorded once
答案4
得分: 0
*//寻找两个数组中的重复元素
public static void main(String[] args) {
Integer arr1[] = {2, 4, 5, 6, 3, 2, 4};
Integer arr2[] = {5, 4, 9, 10, 23, 5, 4};
Set<Integer> set = new HashSet<>();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i] == arr2[j]) {
set.add(arr2[j]);
}
}
}
System.out.println(set); //[4,5]
}*
英文:
*//Finding Duplicates in two Arrays
public static void main(String[] args) {
Integer arr1[]= {2, 4, 5, 6, 3, 2, 4};
Integer arr2[]= {5, 4, 9, 10, 23, 5, 4};
Set<Integer> set=new HashSet<>();
for(int i=0;i<arr1.length;i++) {
for(int j=0;j<arr1.length;j++) {
if(arr1[i]==arr2[j]) {
set.add(arr2[j]);
}
}
}
System.out.println(set);//[4,5]
}*
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论