如何从两个数组中删除重复项?
How do I remove duplicates from two 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。
也许你应该试试下面的代码:
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 class 复制删除 {
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]);
}
}
这是一个例子..我编译了它的工作。如有任何问题,请告诉我 :)
我需要一种算法来更改一个数组中的值(如果它在第二个数组中)。结果是第一个数组不应包含第二个数组中的任何值。
数组的长度是随机的(平均每个数组从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 class 复制删除 {
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]);
}
} 这是一个例子..我编译了它的工作。如有任何问题,请告诉我 :)