List.indexOf only ever tells you where the first match sits. When a value shows up more than once you need every position, not just the first one.
Write a method that returns the zero-based index of every element that matches the target value, in ascending order. Matching is exact and case sensitive, so Apex does not match apex.
Elements that are null are simply skipped. If the list is null, the target is null, or nothing matches, return an empty list.
Examples:
Input: values = ['a', 'b', 'a', 'c', 'a'], target = 'a'
Output: (0, 2, 4)
Explanation: The target sits at three positions and all three are returned in order
Input: values = ['x', 'y', 'z'], target = 'y'
Output: (1)
Explanation: A single match still comes back inside a list
Input: values = ['x', 'y', 'z'], target = 'q'
Output: ()
Explanation: Nothing matches so the list comes back empty
Input: values = ['Apex', 'apex'], target = 'apex'
Output: (1)
Explanation: Matching is case sensitive so only the lowercase entry counts
Input: values = null, target = 'a'
Output: ()
Explanation: A null list returns an empty list rather than throwing
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
Contest runs December 1 - 31. Complete challenges to climb the leaderboard!
Prize 1: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.