Skip to content

Commit e06c5eb

Browse files
committed
Add solution #2634
1 parent b1e4cc0 commit e06c5eb

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
2627|[Debounce](./2627-debounce.js)|Medium|
387387
2629|[Function Composition](./2629-function-composition.js)|Easy|
388388
2630|[Memoize II](./2630-memoize-ii.js)|Hard|
389+
2634|[Filter Elements from Array](./2634-filter-elements-from-array.js)|Easy|
389390
2637|[Promise Time Limit](./2637-promise-time-limit.js)|Medium|
390391
2648|[Generate Fibonacci Sequence](./2648-generate-fibonacci-sequence.js)|Easy|
391392
2649|[Nested Array Generator](./2649-nested-array-generator.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2634. Filter Elements from Array
3+
* https://leetcode.com/problems/filter-elements-from-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array arr and a filtering function fn, return a filtered array filteredArr.
7+
*
8+
* The fn function takes one or two arguments:
9+
* arr[i] - number from the arr
10+
* i - index of arr[i]
11+
*
12+
* filteredArr should only contain the elements from the arr for which the expression
13+
* fn(arr[i], i) evaluates to a truthy value. A truthy value is a value where
14+
* Boolean(value) returns true.
15+
*
16+
* Please solve it without the built-in Array.filter method.
17+
*/
18+
19+
/**
20+
* @param {number[]} arr
21+
* @param {Function} fn
22+
* @return {number[]}
23+
*/
24+
var filter = function(arr, fn) {
25+
const result = [];
26+
27+
for (let i = 0; i < arr.length; i++) {
28+
if (fn(arr[i], i)) {
29+
result.push(arr[i]);
30+
}
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)