簡體   English   中英

從Java中的2D數組中刪除重復項

[英]Remove duplicates from 2D array in Java

因此,我有兩個多維數組。

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

您可以嘗試基於哈希表的算法,即為每個矩陣向量計算哈希值,並使用哈希鍵將向量索引保存在哈希圖中。 然后根據哈希表索引值構造一個結果矩陣。 例如:

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

}

在大型矩陣上仍然可能發生哈希未命中,因此使用了Google Guava提供的MurMur3A代替了Arrays.hashCode(all[i])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM