Advertisement
Hey_Donny

lastIndexOf

Apr 11th, 2022
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. /**
  2. * Finds the index of the last occurrence of <code>target</code> in <code>source</code>
  3. * @param source the array that we are checking.
  4. * @param target the element which index we are looking for
  5. * @return integer index of target
  6. *
  7. * @author Dimitar Naydenov
  8. */
  9. public static int lastIndexOf(int[] source, int target) {
  10. if (source.length == 0){
  11. return -1;
  12. }
  13. for (int i = source.length-1; i >= 0 ; i--) {
  14. if (source[i] == target){
  15. return i;
  16. }
  17. }
  18. return -1;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement