Implement Array.filter

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Array.filter

Approach

Iterate through the array and invoke the callback predicate for each element. If the predicate returns a truthy value, push the original element into a result array. Elements that fail the test are skipped, producing a subset of the original array.

Complexity Analysis

Time
O(n)
Space
O(n)

Pattern

Array Polyfill

Why It Works

Filter tests each element against a predicate and includes only those that pass. Passing Boolean as the callback is a common idiom to remove all falsy values (0, "", null, undefined, false).

Updated Feb 2026