public class Test4 { public static int findFirst(int[] a,int key){ int index = 0; while (a[index]<=key) { if (a[index] == key) { return index; } index += key - a[index]; } return -1; } public static void main(String[] args) { int[] a = {4, 5, 6, 5, 6, 7, 8, 9, 10, 9}; System.out.println(findFirst(a,5)); } }