Let's assume the following array of integers:
int[] numbers = {4, 5, 1, 3, 7, 4, 1};
Since this is an array of primitives, the solution can simply loop the array and return to the first occurrence of the given integer, as follows:
public static boolean containsElement(int[] arr, int toContain) { for (int elem: arr) { if (elem == toContain) { return true; } } return false;}
Another solution to this problem can rely on the Arrays.binarySearch() methods. There are several flavors of this method, but in this case, we need this one: int binarySearch(int[] a, int key). The method will search the given key in the given array and will return the corresponding index or a negative value. The only issue is that this method ...