【问题标题】:Remove duplicates from 2D array in Java从Java中的二维数组中删除重复项
【发布时间】:2019-05-02 17:03:03
【问题描述】:

所以,我有两个多维数组。

    double[][] combinations = new double[10000][3];
    double[][] uniqueCombinations = new double[100][3];

数组值示例:

[[1.233, 1.333, 0.76], [1.1, 1.333, 1.333], [0.9, 1.1, 0.9], [1.1, 1.333, 1.333]]

这就是我想要的

[[1.233, 1.333, 0.76], [1.1, 1.333, 1.333], [0.9, 1.1, 0.9]]

我想从组合中获取所有唯一数组并用它填充 uniqueCombinations。

我试过这个函数,但它只填充了 5 个数组,很奇怪!

public static double[][] removeDuplicate(double[][] matrix) {
    double[][] newMatrix = new double[matrix.length][matrix[0].length];
    int newMatrixRow = 1;

    for (int i = 0; i < matrix[0].length; i++)
        newMatrix[0][i] = matrix[0][i];

    for (int j = 1; j < matrix.length; j++) {
        List<Boolean> list = new ArrayList<>();
        for (int i = 0; newMatrix[i][0] != 0; i++) {
            boolean same = true;
            for (int col = 2; col < matrix[j].length; col++) {
                if (newMatrix[i][col] != matrix[j][col]) {
                    same = false;
                    break;
                }
            }
            list.add(same);
        }

        if (!list.contains(true)) {
            for (int i = 0; i < matrix[j].length; i++) {
                newMatrix[newMatrixRow][i] = matrix[j][i];
            }
            newMatrixRow++;
        }
    }

    int i;
    for(i = 0; newMatrix[i][0] != 0; i++);

    double finalMatrix[][] = new double[i][newMatrix[0].length];
    for (i = 0; i < finalMatrix.length; i++) {
        for (int j = 0; j < finalMatrix[i].length; j++)
            finalMatrix[i][j] = newMatrix[i][j];
    }

    return finalMatrix;
}

【问题讨论】:

  • TLDR;你知道组合不是有序的吗? IE。 [1.5, 2.0][2.0, 1.5] 一样吗?
  • 不,它没有被订购。它需要不订购,因为它是一个组合。
  • 好的。检查这个code 的样本,它可能对你有用......

标签: java arrays object arraylist multidimensional-array


【解决方案1】:

您可以尝试基于哈希表的算法,即计算每个矩阵向量的哈希值并使用哈希键将向量索引保存在哈希图中。然后根据哈希表索引值构造一个结果矩阵。例如:

   import static org.junit.Assert.assertArrayEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.junit.Test;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;

public class ArraysCombination {

    private static double[][] COMBINATIONS = { 
            {1.233, 1.333, 0.76 }, 
            { 1.1, 1.333, 1.333 }, 
            { 0.9, 1.1, 0.9 },
            { 1.1, 1.333, 1.333 } };


    private static double[][] uniqieCombinations(double[][] all) {
        final Map<Integer,Integer> uniqueIdx = new HashMap<>();
        // hashing can be replaced with Arrays.hashCode(all[i])
        final HashFunction hashFunction = Hashing.murmur3_32(all.length);
        for (int i = 0; i < all.length; i++) {
            final Hasher hasher = hashFunction.newHasher();
            for (int j = 0; j < all[i].length; j++) {
                hasher.putDouble(all[i][j]);
            }
            final Integer hash = hasher.hash().asInt();
            if( !uniqueIdx.containsKey(hash) ) {
                uniqueIdx.put(hash, Integer.valueOf(i));
            } 
        }
        double[][] arr = new double[uniqueIdx.size()][];
        Iterator<Integer> it = uniqueIdx.values().iterator();
        for (int i=0; i < arr.length; i++ ) {
            int idx = it.next();
            arr[i] = Arrays.copyOf( all[ idx ], all[idx].length  );
        }
        return arr;
    }



    @Test
    public void shouldFindUniqueCombinations() {
        double [][] uniqueCombination = uniqieCombinations(COMBINATIONS);
        for (double[] ds : uniqueCombination) {
            System.out.println(Arrays.toString(ds));
        }
        double[][] expected  = {{1.233, 1.333, 0.76}, {1.1, 1.333, 1.333}, {0.9, 1.1, 0.9}};
        for (int i = 0; i < expected.length; i++) {
            assertArrayEquals("Wrong unique combinations", expected[i] , uniqueCombination[i], 0 );
        }
    }

}

在巨大的矩阵上hash miss还是有可能的,所以谷歌提供的MurMur3A用Guava代替Arrays.hashCode(all[i])

【讨论】:

  • idx 未找到,我应该用 i 替换它吗?
  • @Tahmid - 抱歉忘记复制一行。刚刚从 IDE 替换了整个文件
猜你喜欢
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 2013-12-18
  • 2023-01-11
相关资源
最近更新 更多