Saturday, 23 April 2016

How to check the presence of a number in array ?


Problem
Check the presence of a number in an integer array

Conditions
There can be up to 1 million items
Solution (code)
public static boolean ifExists(int[] array, int element) {
  int ans = Arrays.binarySearch(array, element);
  return ans > 0;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.