简体   繁体   中英

finding multiple matching numbers in a 2d array and returning a boolean array

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.

The method should utilize the searchForMultiple method.

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: ");
        printGrid(founds);
    }
    public static boolean[][] gridOfMultiples(int[][] testCase){
        boolean [][] gridOfMultiples = new boolean[testCase`.length];
        for(int i = 0; i<testCase.length; i++){
            for(int j =0; j<testCase[i].length; j++){
                if(testCase[i][j]==testCase[i][j]){
                    gridOfMultiples[i][j]= true;
                }
                else
                gridOfMultiples[i][j]= false;
            }
        }
    }
    public static boolean searchForMultiple(int[][] inputArray, int search){
    }
    public static void printGrid(boolean[][] inputGrid){
    }
}

Seems your code could be as simple as this, with no need for a searchForMultiple() method, which would just have been bad for performance anyway.

public static void main(String[] args) {
    int[][] testCase = { {4, 9, 10, 9},
                         {5, 4, 7, 10, 11},
                         {4, 2} };
    System.out.println(Arrays.deepToString(testCase));
    boolean[][] founds = gridOfMultiples(testCase);
    System.out.println(Arrays.deepToString(founds));
}
public static boolean[][] gridOfMultiples(int[][] testCase) {
    Map<Integer, Long> frequency = Stream.of(testCase)
            .flatMapToInt(IntStream::of)
            .boxed()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    boolean[][] founds = new boolean[testCase.length][];
    for (int i = 0; i < testCase.length; i++) {
        founds[i] = new boolean[testCase[i].length];
        for (int j = 0; j < testCase[i].length; j++)
            founds[i][j] = frequency.get(testCase[i][j]) > 1;
    }
    return founds;
}

Output

[[4, 9, 10, 9], [5, 4, 7, 10, 11], [4, 2]]
[[true, true, true, true], [false, true, false, true, false], [true, false]]

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