检查两个int数组是否具有重复元素,并从中提取重复元素之一

莱斯·贾斯

我正在尝试编写一个方法union(),它将返回一个int数组,它需要两个int数组参数并检查它们是否为set,或者换句话说,它们之间有重复项。我编写了另一个方法isSet(),它使用一个数组参数并检查数组是否为集合。问题是我想检查union方法中的两个数组之间是否有重复项,如果有,我想提取其中一个重复项并将其放入unionArray [] int数组中。这是我到目前为止尝试过的。

public int[] union(int[] array1, int[] array2){
  
  int count = 0;
  if (isSet(array1) && isSet(array2)){
     for (int i = 0; i < array1.length; i++){
        for (int j = 0; j < array2.length; j++){
           if (array1[i] == array2[j]){ 
              System.out.println(array2[j]);
              count ++;
           }
        }
     }
  }
  int[] array3 = new int[array2.length - count];
     
  int[] unionArray = new int[array1.length + array3.length];
  int elementOfUnion = 0;
      
  for (int i = 0; i< array1.length; i++){
     unionArray[i] = array1[i];
     elementOfUnion = i + 1 ;
  }
  int index = 0;
  for (int i = elementOfUnion; i < unionArray.length; i++){
     unionArray[i] = array3[index];
     index++;
  }
  
  
  return unionArray;
}


public boolean isSet(int[] array){
  boolean duplicates = true;
  
  for (int i = 0; i < array.length; i++){
     for(int n = i+1; n < array.length; n++){
        if (array[i] == array[n])
           duplicates = false;
     }
  }
     
  return duplicates;
}

我想做的是使用unionArray中的所有array1元素,检查array2是否与array1重复,然后将所有非重复元素从array2移到新的array3,然后将array3连接到unionArray。

阿文德·库玛·阿维纳什(Arvind Kumar Avinash)

使用CollectionAPI或StreamAPI可以轻松得多但是,您已经提到过,您只想使用数组而不导入任何类来完成它,这将需要一些冗长(尽管很简单)的处理单元。驱动逻辑的最重要理论是联合的计算方式(如下所示):

n(A U B) = n(A) + n(B) - n(A ∩ B)

n(Only A) = n(A) - n(A ∩ B)
n(Only B) = n(B) - n(A ∩ B)

下图描述了此解决方案的高级摘要:

在此处输入图片说明

通过代码本身的注释,可以非常清楚地提及其余逻辑。

public class Main {
    public static void main(String[] args) {
        // Test
        display(union(new int[] { 1, 2, 3, 4 }, new int[] { 3, 4, 5, 6 }));
        display(union(new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }));
        display(union(new int[] { 1, 2, 3, 4 }, new int[] { 1, 2, 3, 4 }));
        display(union(new int[] { 1, 2, 3, 4 }, new int[] { 3, 4 }));
        display(union(new int[] { 1, 2, 3, 4 }, new int[] { 4, 5 }));
        display(union(new int[] { 1, 2, 3, 4, 5, 6 }, new int[] { 7, 8 }));
    }

    public static int[] union(int[] array1, int[] array2) {
        // Create an array of the length equal to that of the smaller of the two array
        // parameters
        int[] intersection = new int[array1.length <= array2.length ? array1.length : array2.length];
        int count = 0;

        // Put the duplicate elements into intersection[]
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array2.length; j++) {
                if (array1[i] == array2[j]) {
                    intersection[count++] = array1[i];
                }
            }
        }

        // Create int []union of the length as per the n(A U B) = n(A) + n(B) - n(A ∩ B)
        int[] union = new int[array1.length + array2.length - count];

        // Copy array1[] minus intersection[] into union[]
        int lastIndex = copySourceOnly(array1, intersection, union, count, 0);

        // Copy array2[] minus intersection[] into union[]
        lastIndex = copySourceOnly(array2, intersection, union, count, lastIndex);

        // Copy intersection[] into union[]
        for (int i = 0; i < count; i++) {
            union[lastIndex + i] = intersection[i];
        }

        return union;
    }

    static int copySourceOnly(int[] source, int[] exclude, int[] target, int count, int startWith) {
        int j, lastIndex = startWith;
        for (int i = 0; i < source.length; i++) {
            // Check if source[i] is present in intersection[]
            for (j = 0; j < count; j++) {
                if (source[i] == exclude[j]) {
                    break;
                }
            }

            // If j has reached count, it means `break;` was not executed i.e. source[i] is
            // not present in intersection[]
            if (j == count) {
                target[lastIndex++] = source[i];

            }
        }
        return lastIndex;
    }

    static void display(int arr[]) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i < arr.length - 1 ? arr[i] + ", " : arr[i]);
        }
        System.out.println("]");
    }
}

输出:

[1, 2, 5, 6, 3, 4]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 5, 4]
[1, 2, 3, 4, 5, 6, 7, 8]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除重复元素并从两个数组中取出一个元素

使用两个列表检查一个列表是否包含非重复元素

检查列表是否具有重复元素的功能方法

Java:如何比较两个int []数组的非重复元素?

检查第一个数组是否已排序,是否有连续的重复元素在重复元素的索引处检查第二个数组

在具有最小索引的数组中找到第一个重复元素

合并两个包含重复元素的数据框

在具有重复元素的元组列表中提取显示最大值的字符串

检查数字是否有任何重复元素

在MATLAB中生成具有特定重复元素的数组

通过对数组进行排序来确定数组是否具有重复元素

PL / SQL:遍历XML列并从重复元素中提取值

数组推送,重复元素

将具有重复元素的数组划分为具有唯一元素的数组

是否有一个接受列表并返回该列表中重复元素列表的函数?

列出所有元素,但仅列出重复元素之一?

给定两个数组,如何遍历一个数组并推入第二个数组的重复元素?

计算两个向量的匹配元素,但不包括重复元素

排序重复元素

将重复出现的元素和唯一元素从给定数组中分离为两个包含唯一元素和重复元素的新数组

如何使用golang检查数组中的重复元素?

摇动:如何从对象列表中提取重复元素并在根级别添加一次

Hibernate CollectionOfElements EAGER提取重复元素

如何使用JavaScript添加具有不同元素的数组并添加重复元素值

得到两个数组列表与重复元素之间的区别

创建具有重复元素的字符向量

具有重复元素的列表的 Python 深度复制

创建具有远距重复元素的列表副本

如何删除具有重复元素的行?