Java 如何从两个阵列中删除重复项?

Java 如何从两个阵列中删除重复项?,java,arrays,Java,Arrays,我需要有一个算法来改变一个数组中的值,如果它在第二个数组中。结果是第一个数组中不应有第二个数组中的任何值 数组的平均长度为0到15个整数,每个数组的内容是一个排序数列表,范围为0到90 public void clearDuplicates(int[] A, int[] B){ for(int i = 0; i < A.length; i++){ for(int j = 0; j < B.length; j++) if(A[i] ==

我需要有一个算法来改变一个数组中的值,如果它在第二个数组中。结果是第一个数组中不应有第二个数组中的任何值

数组的平均长度为0到15个整数,每个数组的内容是一个排序数列表,范围为0到90

public void clearDuplicates(int[] A, int[] B){
    for(int i = 0; i < A.length; i++){
        for(int j = 0; j < B.length; j++)
            if(A[i] == B[j])
                A[i]++;
    }
}

我当前的代码没有清除所有的重复项。除此之外,它可能会创建一个超出范围的索引,或者内容可能超过90。

也许您应该尝试以下代码:

public void clear (int[] A, int[] B)
{
  for (int i=0; i<A.length;i++)
  {
     for (int j=0; j<B.length; j++)
         if(A[i]==B[j])
         {
           for (int k=i; k<A.length;k++)
           A[k]=A[k+1];
           j=B.length-1; //so that the cycle for will not be executed
    }
  }
}

虽然你的问题不是很清楚,但这可能会起作用。假设:

A和B中的整数数小于90。 之后未对数组A进行排序。如果希望,请使用Arrays.sort 解决这个问题。 之后,数组A本身可能包含重复项

public void clearDuplicates(int[] A, int[] B) {
    // Initialize a set of numbers which are not in B to all numbers 0--90
    final Set<Integer> notInB = new HashSet<>();
    for (int i = 0; i <= 90; i++) {
        notInB.add(i);
    }
    // Create a set of numbers which are in B. Since lookups in hash set are
    // O(1), this will be much more efficient than manually searching over B
    // each time. At the same time, remove elements which are in B from the
    // set of elements not in B.
    final Set<Integer> bSet = new HashSet<>();
    for (final int b : B) {
        bSet.add(b);
        notInB.remove(b);
    }

    // Search and remove duplicates
    for (int i = 0; i < A.length; i++) {
        if (bSet.contains(A[i])) {
            // Try to replace the duplicate by a number not in B
            if (!notInB.isEmpty()) {
                A[i] = notInB.iterator().next();
                // Remove the added value from notInB
                notInB.remove(A[i]);
            }
            // If not possible, return - there is no way to remove the
            // duplicates with the given constraints
            else {
                return;
            }
        }
    }
}

您只需使用int[]就可以做到这一点,尽管它有点麻烦。唯一的限制是B本身中可能没有重复项

public void clearDuplicates(int[] A, int[] B) {
    //Number of duplicates
    int duplicate = 0;
    //First you need to find the number of duplicates
    for (int i = 0; i < A.length; i++) {
        for (int j = 0; j < B.length; j++)
            if (A[i] == B[j]) 
                duplicate++;
    }
    //New A without duplicates
    int[] newA = new int[A.length-duplicate];
    //For indexing elements in the new A
    int notDuplicate = 0;
    //For knowing if it is or isn't a duplicate
    boolean check;
    //Filling the new A (without duplicates)
    for (int i = 0; i < A.length; i++) {
        check = true;
        for (int j = 0; j < B.length; j++) {
            if (A[i] == B[j]) {
                check = false;
                notDuplicate--;//Adjusting the index
            }
        }
        //Put this element in the new array
        if(check)
            newA[notDuplicate] = A[i];

        notDuplicate++;//Adjusting the index
    }
}

删除公共类重复项{

public static void main(String[] args) {
    int[] A = { 1, 8, 3, 4, 5, 6 };
    int[] B = { 1, 4 };
    print(clear(A, B));
}

public static int[] clear(int[] A, int[] B) {
    int a = 0;
    for (int i = 0; i < A.length; i++) {
        for (int j = 0; j < B.length; j++) {
            if (A[i] == B[j]) {
                a++;
                for (int k = i; k < A.length - a; k++) {
                    A[k] = A[k + 1];
                }
            }
        }

    }
    int[] C = new int[A.length - a];
    for (int p = 0; p < C.length; p++)
        C[p] = A[p];
    return C;

}

public static void print(int[] A) {
    for (int i = 0; i < A.length; i++)
        System.out.println("Element: " + A[i]);
}
}
下面是一个例子。。我编译并运行了它。如果有任何问题,请告诉我:

你所说的清晰是什么意思?删去为什么不列出呢?A[i]+;-为什么要增加A[i]?@almas和clear我的意思是:用一个新的整数替换重复的值,一个不会出现在另一个数组中的整数。当A.length+B.length>90时,您希望如何处理这种情况?@maroun我这样做是因为我看不到改变值的方法。因为我希望它是一个随机整数。你有没有试着编译代码并运行它?你的方括号让我有点哭。我没有在最后一个for循环中添加方括号,因为它只执行一行。你的if语句看起来不像是闭合的,但它是闭合的,只是因为你没有在for循环中实际使用一行。你甚至不清楚你的代码打算做什么。它从第一个数组中得到一个元素,它是A[i],并将它与B数组的所有元素进行比较。一旦找到重复的元素,它就会执行for循环,该循环打算删除该元素。希望现在清楚了。不,不是。如果你修改了你的代码,会更清晰。谢谢!使用一个不使用数字的集合做了一个棘手的事情,很可能一个位集合比集合更合适。是的,但是因为性能和效率不是问题,所以我使用集合来提高代码的可读性。