How to remove repetition in output results in this code

You could put all your lines in a java.util.LinkedHashSet<String>. A Set means it will only keep distinct items. The reason we’ll use a LinkedHashSet instead of a regular HashSet is to retain the input-order.

After adding them all to the set, we print them.

Here a modification of your code:

int[][] arr = { 
        { 5, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },             
        { 0, 5, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 5, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 5, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0 }, 
        { 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0 },        
        { 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 },  
        { 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -1, 0, 0, 0 },  
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, -1, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 }, 
};
java.util.Set<String> outputLines = new java.util.LinkedHashSet<>();
int position = 0;
for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
        if (arr[i][j] == 1) {
            position = j;

            if (arr[position][j] == 5)
                outputLines.add("Valid Static Node V" + j);
        }
        if (arr[i][j] == -1) {
            position = j;

            if (arr[position][j] == 5)
                outputLines.add("InValid Static Node V" + j);
        }
        if (arr[i][j] == -1) {
            position = j;

            if (arr[position][j] == 55)
                outputLines.add("InValid Mobile Node V" + j);
        }
        if (arr[i][j] == 1) {
            position = j;

            if (arr[position][j] == 55)
                outputLines.add("Valid Mobile Node V" + j);  
        }
    }
}

for(String outputLine : outputLines){
  System.out.println(outputLine);
}

Which will output:

Valid Static Node V1
Valid Static Node V2
Valid Static Node V3
Valid Static Node V4
InValid Static Node V5
InValid Static Node V6
Valid Static Node V7
Valid Static Node V8
Valid Static Node V9
Valid Mobile Node V10
InValid Static Node V11
InValid Mobile Node V12
InValid Static Node V16
InValid Static Node V13
Valid Static Node V14
InValid Static Node V15
InValid Static Node V17
InValid Static Node V18

Try it online.


You can also simplify your if-checks inside the nested loop a bit. For example:

position = j;
if (arr[i][j] == 1) {
    if (arr[position][j] == 5)
        outputLines.add("Valid Static Node V" + j);
    else if(arr[position][j] == 55)
        outputLines.add("Valid Mobile Node V" + j);
}
else if (arr[i][j] == -1) {
    if (arr[position][j] == 5)
        outputLines.add("InValid Static Node V" + j);
    else if (arr[position][j] == 55)
        outputLines.add("InValid Mobile Node V" + j);
}

Try it online.



Since OP later on asked for a solution without any libraries whatsoever, here an alternative approach:

// Loop over the diagonal
for(int diag = 0; diag < arr.length; diag++){
  String nodeType = arr[diag][diag] == 5?
                     "Static"
                    :
                     "Mobile"; // Assumes the nodeType is ALWAYS one of 5 or 55
  // Loop over the rows above the diagonal for the current column
  int node = 0;
  for(int row = 0; row < diag; row++){
    // Bitwise-OR the node with the current value
    //  0  | 0 remains 0
    //  0  | 1 becomes 1
    //  0  | -1 becomes -1
    //  1  | 1 remains 1
    //  -1 | -1 remains -1
    //  (NOTE: 1 | -1 becomes -1, but I assumed a column will never contain both a 1 and -1)
    node |= arr[row][diag];
  }
  String nodeValid = node == 1?
                      "Valid" 
                     :node == -1?
                      "Invalid"
                     :
                      null;
  // Only print the node if the column contained a 1 or -1:
  if(nodeValid != null)
    System.out.println(nodeValid + " " + nodeType + " Node V" + diag);
}

Try it online.

Browse More Popular Posts

Leave a Comment