Skip to content

Practice Filter Elements from Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2634
LeetCode Problem

Filter Elements from Array

Problem Statement

Return arr entries in their original order for which fn(arr[i],i) is truthy, meaning conversion with Boolean returns true. Pass each value and its original index to fn; callbacks may ignore the second argument. Implement this without Array.filter.

Source: Filter Elements from Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
Output
[20,30]

# Example 2

Input
arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
Output
[1]

# Example 3

Input
arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
Output
[-2,0,1,2]

Constraints

0 <= arr.length <= 1000
-10^9 <= arr[i] <= 10^9

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.