Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Find Occurrences Of An Element In An Array

Return the positions where a target value appears in an array.

Acceptance 0%
Problem Statement

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.

Input Format

  • An integer array arr
  • An integer x representing the target value

Output Format

  • A list of all 0-based indices i such that arr[i] == x
  • If the target does not appear, return an empty list

Constraints

  • 1 <= arr.length is assumed for practice settings
  • Elements may be negative, zero, or positive
  • Return indices in increasing order
Examples
Sample cases returned by the problem API.

Example 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

Unlock deeper context for this problem

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.