Finding if an array contains all elements in another array

后端 未结 5 1178
旧时难觅i
旧时难觅i 2020-11-29 10:53

I am trying to loop through 2 arrays, the outer array is longer then the other. It will loop through the first and if the 2nd array does not contain that int it will return

5条回答
  •  感情败类
    2020-11-29 11:23

    You have two options using java.util.Arrays if you don't want to implement it yourself:

    • Arrays.toList(array).contains(x) which does exactly you are doing right now. It is the best thing to do if your array is not guaranteed to be sorted.
    • Arrays.binarySearch(x,array) provided if your array is sorted. It returns the index of the value you are search for, or a negative value. It will be much, much faster than regular looping.

提交回复
热议问题