1

How to check whether all elements in two arrays are the same?

How to make it return true if all values are the same?

I have tried different ways but couldn't succeed.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
smoknovo
  • 19
  • 2
  • 4
    You have to compare the elements and `return false` if you find a pair of non identical elements or `return true` after all elements have been checked and found identical. – deHaar Oct 30 '19 at 07:06
  • 1
    Why did you remove your code? Never edit your question in a way that invalidates existing answers. – Zabuzard Oct 30 '19 at 19:17

4 Answers4

4

Probably try something like this:

public static boolean checkIdentical(int[][] targetArray) {
    for (int i = 1; i < targetArray.length; i++) {
        for (int j = 0; j < targetArray[i].length; j++) {
            if (targetArray[i][j] != targetArray[0][j]) {
                return false;
            }
        }
    }
    return true;
}

Caveat:

If the arrays can have variable lengths like this:

int[][] identicalArray = {{3, 3, 3, 4}, {3, 3, 3}};

Then the condition will be:

if (targetArray[i].length != targetArray[0].length
        || targetArray[i][j] != targetArray[0][j]) {
    return false;
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
4

You can do in a single loop with Arrays.equals() method.

public static boolean checkIdentical(int[][] targetArray) {
    int[] prev = null;
    for (int[] a : targetArray) {

        if (prev != null && !Arrays.equals(a, prev))
            return false;

        prev = a;
    }
    return true;
}

need to import java.util.Arrays.

Leni
  • 653
  • 1
  • 6
  • 23
  • 1
    But there will be a loop inside `Arrays.equals()`. So technically it isn't a single loop solution. – Mushif Ali Nawaz Oct 30 '19 at 07:44
  • 2
    There should be an inside loop I suppose too. But why reinvent the wheel by writing the clumsy looping code again? Agreed that both might have same time complexity. Thanks for pointing out. – Leni Oct 30 '19 at 07:50
1

A possible solution here:

public static void main(String[] args) {
    int[][] identicalArray = { { 3, 3, 3 }, { 3, 3, 3 } };
    int[][] nonIdenticalArray = { { 1, 2, 3 }, { 3, 2, 1 } };

    System.out.println("identicalArray all identical? " + checkIdentical(identicalArray));
    System.out.println("nonIdenticalArray all identical? " + checkIdentical(nonIdenticalArray));
}


public static boolean checkIdentical(int[][] targetArray) {
    int[] array1 = targetArray[0];
    for(int[] array : targetArray) {
        if (!Arrays.equals(array1, array))
            return false;
    }
    return true;
}

What happens in checkIdentical :

  • we receive a 2-dimensional targetArray in the parameter
  • We store first 1-dimensional array of targetArray in array1. This will be our reference array which we will compare with each 1-dimensional array in targetArray
  • We loop through each 1-dimensional array in targetArrayand compare it with array1
  • If a 1-dimensional array does not match our reference array array1, return false
  • If we get to the end of the loop without encountering unequal 1-dimensional arrays, return true

Hope this helps.

ieggel
  • 891
  • 6
  • 12
0

You can use Stream API and Arrays#equals to write elegant 1 line checkIdentical:

public static boolean checkIdentical(int[][] targetArray) {
    return Stream.of(targetArray).allMatch(elm -> Arrays.equals(targetArray[0], elm));
}
IlyaMuravjov
  • 2,352
  • 1
  • 9
  • 27