public static int searchLast ( short [ ] shortArray , short value , int occurrence ) { if ( occurrence <= 0 || occurrence > shortArray . length ) { throw new IllegalArgumentException ( "Occurrence must be greater or equal to 1 and less than " + "the array length: " + occurrence ) ; } int valuesSeen = 0 ; for ( int i = shortArray . length - 1 ; i >= 0 ; i -- ) { if ( shortArray [ i ] == value ) { valuesSeen ++ ; if ( valuesSeen == occurrence ) { return i ; } } } return - 1 ; }
Search for the value in the short array and return the index of the first occurrence from the end of the array .