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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 44ms, memory 53MB, accepted 2026-01-01.
/**
* @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;
};