The easiest method that comes to mind for searching if an array contains a specific value is by coding it using for loop. Here is an example:
public class Test { public static boolean contains(int[] arr, int item) { for (int n : arr) { if (item == n) { return true; } } return false; } public static void main(String[] args) { int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }The for loop is used to go through each item of the array one by one and compare. True is returned if one matches the item, otherwise false is returned. The output of the code will be:
true falseBecause 13 is in the given array, while 25 is not. Note that this is not a very efficient solution since the code may potentially compare all items in the array.
The Arrays.binarySearch can also be used to check if a given value exists in an array. This is a more efficient solution that the code above because the number of comparison is minimal. The benefit of binary search becomes more apparent when the size of the array becomes larger. You may check the explanation of binary search in Wikipedia. Below is an example of how to check is an array contains a value using binary search:
import java.util.Arrays; public class Test { public static boolean contains(int[] arr, int item) { int index = Arrays.binarySearch(arr, item); return index >= 0; } public static void main(String[] args) { int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; Arrays.sort(myArray); System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }The binary search gives the index of the item in the array. If the index is negative, it means the item is not found. Otherwise, it means it exist in the array. Note that binary search have one big assumption. The array is ASSUMED TO BE SORTED for it to return a correct result. Hence the code "Arrays.sort(myArray);" is very important prior to calling binary search. If we ommit this, the result will be unpredictable. For example:
import java.util.Arrays; public class Test { public static boolean contains(int[] arr, int item) { int index = Arrays.binarySearch(arr, item); return index >= 0; } public static void main(String[] args) { int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }
The call to sort was taken out and the output will not be correct. The code above will display:
false falseWhich is not correct.
import java.util.Arrays; import java.util.List; public class Test { public static boolean contains(Integer[] arr, Integer item) { List<Integer> list = Arrays.asList(arr); return list.contains(item); } public static void main(String[] args) { Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }
Similar to List, a Set also have the method contains to check if it has a certain value. We can also use this for checking if an array contains a certain value. For example:
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class Test { public static boolean contains(Integer[] arr, Integer item) { List<Integer> list = Arrays.asList(arr); Set<Integer> set = new HashSet<Integer>(list); return set.contains(item); } public static void main(String[] args) { Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }A Set is conceptually more efficient than List because it does not contain duplicate values. It should use fewer comparison than a List.
If you are using Java 8 or higher, using stream is another option for finding if an item exist in the array. Here is a sample code:
public class Test { public static boolean contains(Integer[] arr, Integer item) { return Arrays.stream(arr).anyMatch(item::equals); } public static void main(String[] args) { Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 }; System.out.println(contains(myArray, 13)); System.out.println(contains(myArray, 25)); } }