java - java中如何查找二维字符串数组中的唯一元素?

标签 java algorithm sorting

我有一个二维字符串数组。这是一个矩阵。我需要对此矩阵进行排序,并将唯一项保存在其他矩阵的第一行中。如何仅使用自己的算法来执行此操作。我的意思是不调用方法,而是编写循环本身来对数组的元素进行排序和比较

  import java.util.Scanner;

    public class Coursework {

public static void main(String[] args) {
    final int linesOfMatrix; //number of lines in the matrix

    System.out.println("Enter number of lines: ");

    Scanner sc = new Scanner(System.in);
    linesOfMatrix = sc.nextInt();       
    Scanner sc2 = new Scanner(System.in);

    String [][] matrix = new String [linesOfMatrix][]; // declare the Matrix

    for(int i=0; i < matrix.length; i++) {
        System.out.println("Enter a value for the string " + (i+1) + " 
    through a space");
        matrix[i] = sc2.nextLine().split(" ");
    }
    sc.close();
    sc2.close(); 

            //below must be unique sort, but he dosen't work rigth

    for(int i=0; i < matrix.length; i++){   
        for(int j=0; j < matrix[i].length-1; j++){
            if(matrix[i][j].equals(matrix[i][j+1])){
                matrix[i][j+1] = matrix[i][j+1];
            }


        } 
    }
        System.out.println("Matrix");
        for(int i=0; i < matrix.length; i++){
            for(int j=0; j < matrix[i].length-1; j++){

                System.out.println("[" +(i) + "][" + (j) + "]= " + matrix[i]
    [j] + " [" + (i) + "][" + (j+1) + "]= " + matrix[i][j+1]  );
            }

        }
    }
    }

最佳答案

使用 Map 来计数元素怎么样:

public static String[] getUnique(String[][] matrix) {
    Map<String, Integer> map = new LinkedHashMap<>();

    for (String[] row : matrix)
        for (String col : row)
            map.put(col, map.getOrDefault(col, 0) + 1);

    List<String> unique = new ArrayList<>();

    for (Map.Entry<String, Integer> entry : map.entrySet())
        if (entry.getValue() == 1)
            unique.add(entry.getKey());

    return unique.toArray(new String[unique.size()]);
}

如果您不想使用Map,那么您可以做同样的事情,但速度要慢一点:

public static String[] getUnique(String[][] matrix) {
    List<String> unique = new ArrayList<>();

    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] == null)
                continue;

            boolean foundUnique = true;

            for (int i = row; i < matrix.length; i++) {
                for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
                    if (matrix[i][j] == null || (i == row && j == col))
                        continue;

                    if (matrix[i][j].equals(matrix[row][col])) {
                        foundUnique = false;
                        matrix[i][j] = null;
                    }
                }
            }

            if (foundUnique)
                unique.add(matrix[row][col]);
            else
                matrix[row][col] = null;
        }
    }

    return unique.toArray(new String[unique.size()]);
}

或者甚至不使用List:-):

public static String[] getUnique(String[][] matrix) {
    int total = 0;

    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] == null)
                continue;

            boolean foundUnique = true;

            for (int i = row; i < matrix.length; i++) {
                for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
                    if (matrix[i][j] == null || (i == row && j == col))
                        continue;

                    if (matrix[i][j].equals(matrix[row][col])) {
                        foundUnique = false;
                        matrix[i][j] = null;
                    }
                }
            }

            if (foundUnique)
                total++;
            else
                matrix[row][col] = null;
        }
    }

    if (total == 0)
        return new String[0];

    String[] res = new String[total];

    for (int row = 0, i = 0; row < matrix.length; row++)
        for (int col = 0; col < matrix[row].length; col++)
            if (matrix[row][col] != null)
                res[i++] = matrix[row][col];

    return res;
}

关于java - java中如何查找二维字符串数组中的唯一元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47539387/

相关文章:

java - Maven 次要版本 `x`

java - 从 JNI 返回字符串数组到 android

algorithm - 在任意坐标周围找到半径为 r 的球体中的所有点

algorithm - 计数排序——效率

arrays - 如何在 swift 中根据另一个数组位置对一个数组进行排序?

python - 对字典/列表列表中的多个参数进行排序

java - Java 中枚举内的 EnumSet 修饰符

java - JNoSQL + MongoDB + Wildfly Swarm

java - 执行 "check point inside triangle"算法时出现错误

algorithm - 开发一种将四个笛卡尔坐标转换为平方坐标的算法