在比较数组集合时,如何说明重复项



晚上好,

我的程序正在比较数组的2D Comparable集合,它必须产生一个包含所有常见元素的数组,包括重复值。例如,当比较{{1,1,1}、{1、1、3}、{1,1、1,5}}的集合时,结果必须是{1。问题是为了避免代码重复了一个不应该重复的值。如果列表中包含该值,我必须将其放入if语句中。请不要添加该值。例如,{4,7,9},{4,5,6},{1,3,4}}会导致{4,4},但正确的公共数组是{4}。我对如何在不导致公共数组中出现错误重复的情况下实现计算重复值的目标感到困惑。感谢您的时间和帮助。

导入java.util.Arrays;导入java.util.ArrayList;

公共类CommonElements{

private int comparisons = 0;

public Comparable<?>[] findCommonElements(Comparable<?>[][] collections) {
Comparable<?>[] queryArray = (Comparable<?>[])collections[0]; //first comparison array
Arrays.sort(queryArray); //sort the array
ArrayList<Comparable<?>> commonList = new ArrayList<Comparable<?>>(); //initialize dynamic array
Comparable<?>[] commonArray = new Comparable[0]; //Initiating common array
for(int i= 1; i < collections.length; i++) { //Compare all arrays in collection to queryArray
Comparable<?>[] nextArray = (Comparable<?>[])collections[i]; //initiate a2 as the next array in collections
Arrays.sort(nextArray); //sort the array
for(int j = 0; j < queryArray.length; j++) { //Compare each index in queryArray to elements from other arrays
for(int k = 0; k < nextArray.length; k++) { //traverse nextArray comparing to index in queryArray
if(queryArray[j] == nextArray[k]) {
if(commonList.contains(nextArray[k])) { //avoid adding the value multiple times when compared to more than 1 array
++comparisons; //add to comparison count
break;
}
else {
commonList.add(queryArray[j]); //set new element to commonList
++comparisons; //add to comparison count
break;
}
}
else {
++comparisons; //add to comparisons each time it compares an index
}
}
}
queryArray = commonList.toArray(new Comparable[commonList.size()]); //make the next queryArray equal to the current commonList
}
if(comparisons == 0) {
commonArray = queryArray;
return commonArray; //In case collections has single array
}
else {
commonArray = commonList.toArray(new Comparable[commonList.size()]);
return commonArray; //return the common array when loops finish
}
}
public int getComparisons() {
return comparisons;
}
}

我们可以维护一个计数器数组,用于维护每个数组中元素的计数。然后,我们可以将第一个计数器作为基础,对于计数器映射中的每个键,找到该键在所有数组中出现的最小次数,以找到该键出现在所有数组的最小次数。一旦我们知道了count,我们就可以多次将该键添加到结果集count中。

public Comparable<?>[] findCommonElements(Comparable<?>[][] collections) {
Map<Comparable<?>, Integer>[] counterArray = new Map[collections.length];
List<Comparable<?>> result = new ArrayList<>();
for (int i=0; i<collections.length; i++) {
counterArray[i] = new HashMap<>();
for (int j=0; j<collections[i].length; j++) {
Comparable<?> comparable = collections[i][j];
int newVal = counterArray[i].getOrDefault(comparable, 0);
counterArray[i].put(comparable, newVal+1);
}
}
if (collections.length == 1)
return collections[0];
for (Map.Entry<Comparable<?>, Integer> entry : counterArray[0].entrySet()) {
Comparable<?> comparable = entry.getKey();
int minCount = entry.getValue();
for (int j=1; j<counterArray.length; j++) {
minCount = Math.min(
minCount,
counterArray[j].getOrDefault(comparable, 0)
);
}
for (int count = 1; count <= minCount; count++)
result.add(comparable);
}
return result.toArray(Comparable[]::new);
}
public int getComparisons() {
return comparisons;
}

最新更新