繁体   English   中英

比较 arrays 的集合时如何计算重复项?

[英]How to account for duplicates when comparing collection of arrays?

晚上好,

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

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

公共 class 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;
    }
}

我们可以维护一个计数器数组来维护每个数组中元素的计数。 然后,我们可以将第一个计数器作为基础,对于计数器 map 中的每个键,找到该键在所有 arrays 中出现的最小次数,以找到该键在所有 ZA3CBC3F9D0CE2F2C1554E1D671 中出现的最少次数。 一旦我们知道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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM