0

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

        }
    }
    }
6
  • Could you provide a test case for this? Commented Nov 28, 2017 at 20:40
  • @oleg.cherednik did not understand the question, do you need the input data? Commented Nov 28, 2017 at 20:55
  • @oleg.cherednik this data for input drive.google.com/file/d/191y56tw-WKyM20kRxlMcewrLLCf3RQcl/… Commented Nov 28, 2017 at 21:01
  • Yes, could you give some notes in words. What read underline means e.g? I still do not see logic of transformation. Commented Nov 28, 2017 at 21:27
  • @oleg.cherednik red lines do not mean anything. Commented Nov 28, 2017 at 21:58

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.