Home > Enterprise >  How to check if all elements in one array exist in other array. However in the same order
How to check if all elements in one array exist in other array. However in the same order

Time:03-15

I've been trying to solve the issue of searching through array1 to return a boolean if the elements of array1 can be found in array2. However, it can only be true if array2 contains array1 elements in the same order.

For example, array1 = {3,4,5,6} and array2 = {3, 4, 1, 2, 7, 5, 6} would be true since all elements are found in the same order.

Here's my code so far:

int size = Queue1.size();

for(int i = 0; i < size; i  ) {
    arr3[i] = Queue1.dequeue();
}

boolean test = Arrays.asList(arr2).containsAll(Arrays.asList(arr3));
System.out.println(test);

The variable test returns to be false for the input of

  • arr3 = {3,4,5,6}
  • arr2 = {3,4,2,1,5,10,9,8,6,7}

CodePudding user response:

You are using the right approach, there's a suttle thing that you have to pay attention when using Arrays.asList, look:

If you pass an array of int[] to asList() it will return a List<int[]> rather than a List of Integer.

But if you pass an array of Integer[] to asList() it will return a List of Integer and you will be able to compare it.

See if on code it is more clear:

    public static void main(String[] args) {
    Integer[] arr1 = {1, 2, 3, 4};
    Integer[] arr2 = {1, 2, 3, 4, 5};
    boolean wrappers = checkWrappers(arr2, arr1);
    System.out.println(wrappers); // true

    int[] err1 = {1, 2, 3, 4};
    int[] err2 = {1, 2, 3, 4, 5};
    boolean primitives = checkPrimitives(err2, err1);
    System.out.println(primitives); // false
}

public static boolean checkWrappers(Integer[] outer, Integer[] inner) {
    List<Integer> integers2 = Arrays.asList(outer);
    List<Integer> integers1 = Arrays.asList(inner);
    return integers2.containsAll(integers1);
}

public static boolean checkPrimitives(int[] outer, int[] inner) {
    List<int[]> ints2 = Arrays.asList(outer);
    List<int[]> ints1 = Arrays.asList(inner);
    return ints2.containsAll(ints1); // compare object references not content
}

When passing primitives it compares the int[] object reference instead of comparing each element as you would want to.

CodePudding user response:

This a simple algorithm to resolve your problem, base on List<Integer> contains and indexOf function:

code (with comments):

public static void main(String[] args) {

    Integer[] array1 = new Integer[]{3,4,5,6};
    Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};
    
    List<Integer> list1 =  Arrays.asList(array1);
    List<Integer> list2 =  Arrays.asList(array2);
    
    boolean check = false;
    int i = 0;
    for (Integer n : list1) {
        if (i == 0) {
            if (list2.contains(n)) {// first element check if it's on the second array
                check = true;
            }else check = false;
        }else {
            // check if the element in the array2 and its index is > of the previous element
            if (list2.contains(n) && list2.indexOf(n) > list2.indexOf(list1.get(i-1))) {
                check = true;
            }else {
                check = false;
                break;
                
            }
        }
        i  ;
    }
    System.out.println(check);
}

result:

input 1:

Integer[] array1 = new Integer[]{3,4,5,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};

output 1: true

input 2:

Integer[] array1 = new Integer[]{3,7,4,6};
Integer[] array2 = new Integer[]{3, 4, 1, 2, 7, 5, 6};

output 2: false
  • Related