Return the positions where a target value appears in an array.
Given an integer array and a target value, find every index at which the target appears. Return the matching indices in increasing order.
This is a straightforward array traversal problem that tests careful iteration and basic frequency/index tracking.
arrx representing the target valuei such that arr[i] == x1 <= arr.length is assumed for practice settingsExample 1
Input
arr = [1, 2, 3, 2, 4, 2], x = 2
Output
[1, 3, 5]
Explanation
The value 2 appears at indices 1, 3, and 5.
Example 2
Input
arr = [5, 6, 7], x = 1
Output
[]
Explanation
The target value does not occur in the array.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.