Skip to content

Latest commit

 

History

History
55 lines (44 loc) · 1.08 KB

_2634. Filter Elements from Array.md

File metadata and controls

55 lines (44 loc) · 1.08 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 09, 2024

Last updated : July 09, 2024


Related Topics : N/A

Acceptance Rate : 85.22 %


Solutions

JavaScript

/**
 * @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] : []);
};