Find the Index (Part #2)

Published by Bjarte in

Create a function that searches for the index of a given item in an array using recursion. If the item is present, it should return the index, otherwise, it should return -1.

Examples

Search({1, 2, 3, 4}, 3) ➞ 2

Search({2, 4, 6, 8, 10}, 8) ➞ 3

Search({1, 3, 5, 7, 9}, 11) ➞ -1

Notes

  • Use recursion.
  • Avoid using Linq.
  • If the item is not present, return -1.
  • The given array is ordered.
Watch a quick demo on how Edabit works.