Given an integer array arr and a filtering function fn, return a filtered array filteredArr.
The fn function takes one or two arguments:
arr[i] - number from the arri - index of arr[i]filteredArr should only contain the elements from the arr for which the expression fn(arr[i], i) evaluates to a truthy value. A truthy value is a value where Boolean(value) returns true.
Please solve it without the built-in Array.filter method.
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] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to filtering elements means we check each element individually. For each element, we decide if it should be kept or removed based on a specific rule. This ensures we consider every element in the original collection.
Here's how the algorithm would work step-by-step:
def filter_elements(original_list, condition):
filtered_list = []
# Iterate through each element in the original list.
for element_index in range(len(original_list)):
current_element = original_list[element_index]
# Check if the current element satisfies the condition.
if condition(current_element):
# Add the element to the filtered list.
filtered_list.append(current_element)
return filtered_listThe best way to solve this problem is to carefully select which array elements should be kept based on a specific rule. We go through the array once and decide what to keep, placing the kept items into a new collection.
Here's how the algorithm would work step-by-step:
def filter_elements(original_array, filter_rule):
filtered_array = []
# Iterate through each element in the original array.
for element in original_array:
# Apply the filter rule to determine if the element should be included.
if filter_rule(element):
filtered_array.append(element)
return filtered_array| Case | How to Handle |
|---|---|
| Null or undefined input array | Return an empty array or throw an error depending on desired behavior. |
| Empty input array | Return an empty array as there are no elements to filter. |
| Null or undefined filtering function | Throw an error or return the original array, depending on the desired behavior. |
| Filtering function always returns true | Return a copy of the original array. |
| Filtering function always returns false | Return an empty array. |
| Large input array causing potential performance issues | Ensure the solution has O(n) time complexity and avoids unnecessary memory allocations. |
| Array contains non-integer values or mixed data types | Type-check elements within the filtering function or reject the input depending on the function's expected input types. |
| Filtering function throws an error | Catch the error and handle it appropriately, possibly by logging it and continuing with the next element or returning an error result. |