0

Im having trouble creating a method that takes as input a two-dimensional integer array. The output will be a second two-dimensional array with equal number of elements in the corresponding rows of the first array, this time the type of the elements will be booleans. The values of the output will correspond to the value existing in the array multiple times.

Here I have provided an example:

If inputArray is the output of the method would be the following boolean array: [[ 4, 9], [ 9, 5, 3]]. Since 9 is in the input array twice, both positions in the output that correspond to those two instances are true, all of the other values only occur once, so their values are false like this [[ false, true ], [ true, false, false]].

The code is provided below.

class SearchAndPrint{

public static void main(String[] args){
  int[][] testCase = 
    {{4, 9, 10, 9},
    {5, 4, 7, 10, 11},
    {4, 2}};


System.out.println("The test case array: ");
  for (int i=0; i<testCase.length; i++){
    for (int j=0; j<testCase[i].length; j++){ 
      System.out.print(testCase[i][j]+" ");
    }
    System.out.println();      
  }        

  boolean[][] founds = gridOfMultiples(testCase);
  System.out.println("Positions with repeated numbers are marked below: ");


}

public static boolean[][] gridOfMultiples(int[][] inputArray){
      boolean [][] gridOfMultiples = new boolean[inputArray.length][];
      Arrays.fill(gridOfMultiples, false);
      for(int i=0; i<inputArray.length.length; i++){
        for (int j=0; j<inputArray.length[i]; j++){
          if(inputArray.length[i][j]==inputArray.length[i][j]){
         gridOfMultiples[i][j] = true;
       break;
  }
          return gridOfMultiples;
}


 }
  return whoIsPrime;
 }
3
  • You need to compare each value in the array to all the other values and then set the boolean array appropriately. Then continue onto the next one. Presently, you are just doing a one-to-one comparison. Commented Sep 3, 2020 at 0:48
  • How can I do that im fairly new too arrays ? Commented Sep 3, 2020 at 0:58
  • Your gridOfMultiples method won't compile, as there are several issues with it. You may want to comment it out as pseudo-code so as not to confuse others. You are also comparing one value to itself inputArray.length[i][j] == inputArray.length[i][j] which is unfortunately both useless (it's always true) and invalid (doesn't compile). Commented Sep 3, 2020 at 1:18

1 Answer 1

1

Here is how you would do it for a single D array. Array is iterated twice and each subsequent value of the array is compared to all the others. Note that primitive boolean arrays of known size are automatically initialized to all false values.

int [] vals = {1,2,3,2,4,5,6,4,9,4};
boolean[] bools = new boolean[vals.length];
for (int i = 0; i < vals.length-1; i++) {
    for (int k = i+1; k < vals.length; k++) {
        if (vals[i] == vals[k]) {
            // ensure both are true but continue checking
            // as there may be more duplicate values.
            bools[i] = true; 
            bools[k] = true;
        }
    }
}
System.out.println(Arrays.toString(bools));

Prints

[false, true, false, true, true, false, false, true, false, true]

Only 2 and 4 occur multiple times.

Sign up to request clarification or add additional context in comments.

1 Comment

To add onto this approach for a n-d array solution, you can collapse the array to 1d, and run it through this method, then generate the n-d array using the length of each row from the result. Let me know if not clear, and I can post a solution.

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.