All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 09, 2024
Last updated : July 09, 2024
Related Topics : N/A
Acceptance Rate : 85.22 %
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
var filter = function(arr, fn) {
const output = []
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i], i)) {
output.push(arr[i]);
}
}
return output;
};
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
var filter = function(arr, fn) {
return arr.flatMap((val, i) => fn(val, i) ? [val] : []);
};