LeetCode solutions

2746. Filter Elements from Array

My accepted JavaScript solution to LeetCode problem 2746, Filter Elements from Array, running in 44ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 44ms
  • Memory 53MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 44ms, memory 53MB, accepted 2026-01-01.

javascript
/**
 * @param {number[]} arr
 * @param {Function} fn
 * @return {number[]}
 */
var filter = function(arr, fn) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        if (fn(arr[i], i)) {
            result.push(arr[i]);
        }
    }
    return result;
};

Source