简体   繁体   中英

How to find unique elements in a two-dimensional array of strings in java?

I have a two-dimensional array of String. This is a Matrix. I need to sort this Matrix and save unique items in first line other Matrix.How to do this use only own arlgorithm.I mean do not call a method but write the loop itself that will sort through and compare the elements of the array

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

        }
    }
    }

What about using Map with counting elements:

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

In case you do not want to use Map , then you coudl just do the same with a bit slower:

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

Or even do not use 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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