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

时间:2015-01-03 13:30:09

标签: 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] == B[j])
                A[i]++;
    }
}

我当前的代码并未清除所有重复项。最重要的是,它可能会创建一个超出范围的索引,或者内容可以超过90.

4 个答案:

答案 0 :(得分:1)

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

  1. A和B中的整数数小于90.
  2. 之后不对数组A进行排序(如果您愿意,请使用Arrays.sort() 修复那个)。
  3. 之后,数组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;
                }
            }
        }
    }
    

答案 1 :(得分:0)

你可以通过使用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
    }
}

答案 2 :(得分:0)

public class DuplicateRemove {

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]);
}

} 这是一个例子..我编译和它的工作。对于任何问题,请告诉我:)

答案 3 :(得分:-1)

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

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
    }
  }
}