public static int searchLast ( char [ ] charArray , char value , int occurrence ) { if ( occurrence <= 0 || occurrence > charArray . 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 = charArray . length - 1 ; i >= 0 ; i -- ) { if ( charArray [ i ] == value ) { valuesSeen ++ ; if ( valuesSeen == occurrence ) { return i ; } } } return - 1 ; }
Search for the value in the char array and return the index of the first occurrence from the end of the array .