File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 386
386
2627|[ Debounce] ( ./2627-debounce.js ) |Medium|
387
387
2629|[ Function Composition] ( ./2629-function-composition.js ) |Easy|
388
388
2630|[ Memoize II] ( ./2630-memoize-ii.js ) |Hard|
389
+ 2634|[ Filter Elements from Array] ( ./2634-filter-elements-from-array.js ) |Easy|
389
390
2637|[ Promise Time Limit] ( ./2637-promise-time-limit.js ) |Medium|
390
391
2648|[ Generate Fibonacci Sequence] ( ./2648-generate-fibonacci-sequence.js ) |Easy|
391
392
2649|[ Nested Array Generator] ( ./2649-nested-array-generator.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments