Filter Elements from Array

Updated on 30 May, 2025
Filter Elements from Array header image

Problem Statement

In this task, you are provided with an integer array named arr and a function fn which serves as a filter. Your goal is to create a new array, filteredArr, that only includes elements from arr that satisfy a certain condition set by fn. The function fn might take one or two parameters—the element arr[i] and optionally its index i.

You are to populate filteredArr with those elements from arr for which the function call fn(arr[i], i) returns a truthy value. A truthy value in this context means any value that, when converted to a Boolean, yields true.

This problem restricts the use of JavaScript's built-in Array.filter method. Therefore, you will need to implement the filtering logic manually.

Examples

Example 1

Input:

arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }

Output:

[20,30]

Explanation:

const newArray = filter(arr, fn); // [20, 30]
The function filters out values that are not greater than 10

Example 2

Input:

arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }

Output:

[1]

Explanation:

fn can also accept the index of each element
In this case, the function removes elements not at index 0

Example 3

Input:

arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }

Output:

[-2,0,1,2]

Explanation:

Falsey values such as 0 should be filtered out

Constraints

  • 0 <= arr.length <= 1000
  • -109 <= arr[i] <= 109

Approach and Intuition

The task is to filter the array based on a custom function without using the built-in filter method. Here's a streamlined approach based on the examples provided:

  1. Initialize an empty array filteredArr to hold the results.
  2. Iterate through each element arr[i] of array arr.
  3. For each element, invoke the filter function fn. If fn takes two parameters, pass both the element and its index i.e., fn(arr[i], i). If it takes one parameter, provide only the element i.e., fn(arr[i]).
  4. If the result of fn(arr[i], i) or fn(arr[i]) is truthy, append arr[i] to filteredArr.
  5. Continue this process for the entire array.
  6. Return filteredArr.

For a better understanding, let’s look at specific examples:

  • Example 1: For each element, the function greaterThan10 checks if it's greater than 10. This function requires only one parameter. Filtered results are [20, 30].

  • Example 2: Involves a function firstIndex which checks if the current index i is 0. This requires both the element and its index. The filtered result is [1], as only the first element meets this condition.

  • Example 3: Uses a function plusOne that increments each number by one and filters out any result that isn't truthy (e.g., if the incremented value is 0). This results in [ -2, 0, 1, 2].

With these guidelines, the problem can be solved by simulating the filter process manually, ensuring that only elements fulfilling the custom criteria are returned. This method directly mirrors the built-in filter logic but is applied manually, allowing for detailed control of the filtering condition and adherence to constraints.

Solutions

  • JavaScript
js
var applyFilter = function(data, condition) {
    return data.filter(condition);
};

The provided JavaScript function applyFilter simplifies the process of filtering elements from an array based on a specified condition. Implement this function by passing an array (data) and a condition (a function that defines the filtering criteria).

  • Usage involves creating a filtering function that returns a boolean value
  • Call applyFilter, passing the array and the filtering function as parameters

For example, to filter even numbers from an array:

javascript
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = applyFilter(numbers, num => num % 2 === 0);

This function uses the filter method of the array, which iterates over each item and applies the condition function to determine whether an item should be included in the resulting array.

Comments

No comments yet.