All Occurrences of an Element in an Array

Published by Helen Yu in

Create a function that returns the indices of all occurrences of an item in the array.

Examples

getIndices([1, 5, 5, 2, 7], 7) ➞ [4]

getIndices([1, 5, 5, 2, 7], 5) ➞ [1, 2]

getIndices([1, 5, 5, 2, 7], 8) ➞ []

Notes

  • If an element does not exist in an array, return [].
  • Arrays are zero-indexed.
  • Values in the array will be value-types (don't need to worry about nested arrays).
Watch a quick demo on how Edabit works.