Skip to content

Instantly share code, notes, and snippets.

@nikoncode
Created March 22, 2015 11:24
Show Gist options
  • Select an option

  • Save nikoncode/093bc0aebbfe191345d7 to your computer and use it in GitHub Desktop.

Select an option

Save nikoncode/093bc0aebbfe191345d7 to your computer and use it in GitHub Desktop.
int[] arr = {1,2,7,4,5,7,5,7,6,7,0,-1,22,15};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(i + " : " + arr[i]);
}
System.out.println("RES:");
int match = Arrays.binarySearch(arr, 7);
if (match >= 0) {
int first = match;
int last = match;
while (first > 0 && arr[first - 1] == arr[match])
--first;
while (last < arr.length - 1 && arr[last+1] == arr[match])
++last;
System.out.println("Matches " + first + " " + last);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment