Apply Transform Over Each Element in Array

Updated on 21 May, 2025
Apply Transform Over Each Element in Array header image

Problem Statement

In this task, you are provided with an integer array arr and a mapping function fn. Your goal is to construct a new array where each element is transformed by the given mapping function. Specifically, for each element in the original array, the corresponding element in the new array is determined by the function applied to the original element and its index. The transformation on the i-th element follows the rule returnedArray[i] = fn(arr[i], i). You are required to implement this functionality manually, explicitly without the use of JavaScript's built-in Array.map function or similar methods available in other programming environments.

Examples

Example 1

Input:

arr = [1,2,3], fn = function plusone(n) { return n + 1; }

Output:

[2,3,4]

Explanation:

const newArray = map(arr, plusone); // [2,3,4]
The function increases each value in the array by one.

Example 2

Input:

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

Output:

[1,3,5]

Explanation:

The function increases each value by the index it resides in.

Example 3

Input:

arr = [10,20,30], fn = function constant() { return 42; }

Output:

[42,42,42]

Explanation:

The function always returns 42.

Constraints

  • 0 <= arr.length <= 1000
  • -109 <= arr[i] <= 109
  • fn returns an integer.

Approach and Intuition

Example Analysis

Let's dive deeper into the given examples to understand how the mapping function fn modifies the array elements based on different implementations of fn.

Example 1

  1. The array is [1,2,3] and the function plusOne(n) simply increments each number by 1.
  2. Applying plusOne to each element:
    • plusOne(1) = 2
    • plusOne(2) = 3
    • plusOne(3) = 4
  3. Thus, the output is [2, 3, 4].

Example 2

  1. The array is [1,2,3] and the function plusI(n, i) increments each number by its index.
  2. Applying plusI to each element:
    • plusI(1, 0) = 1 + 0 = 1
    • plusI(2, 1) = 2 + 1 = 3
    • plusI(3, 2) = 3 + 2 = 5
  3. As a result, the output is [1, 3, 5].

Example 3

  1. The array is [10, 20, 30] and the function constant() always returns 42, regardless of the input.
  2. Applying constant to any element results in 42.
  3. Hence, the output for any inputs will be [42, 42, 42].

Intuition

From the examples, it is evident that depending on the functionality of fn, elements of the new array can be derived directly from their corresponding elements in arr or from the combined effect of the element values and their indices. The flexibility of fn allows us to manipulate arrays in versatile manners:

  • Adding constants to elements.
  • Modifying elements based on their positions or indices.
  • Returning a fixed value irrespective of the input.

Given the constraints:

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

the algorithm should efficiently handle arrays up to a length of 1,000 which is considerable but manageable within memory and processing limits of modern computing environments. Furthermore, even though individual operations are simple, the entire process should be optimized for different scenarios to cater up to the higher limits of input sizes and value ranges.

Solutions

  • JavaScript
js
var transform = function(data, callback) {
    for (let index = 0; index < data.length; ++index) {
        data[index] = callback(data[index], index);
    }
    return data;
};

The provided JavaScript function named transform modifies each element in an array based on a transformation defined by a callback function. This function accepts two parameters: data, which is the array to be transformed, and callback, which is a function that defines how each element in the array should be modified.

Here’s how the function operates:

  1. Loops through each element of the array data using a for loop. The loop index index is used to access each element in the array.
  2. Assigns the return value of the callback function to data[index]. The callback function takes two arguments: the current element and its index.
  3. Returns the modified data array after the loop completes.

This function is useful for applying any transformation to the elements of an array where the transformation might depend on the element’s value, its index, or both.

Comments

No comments yet.