JavaScript Program of Absolute Sum of Array Elements
Last Updated :
09 Jun, 2024
Using JavaScript, one can find the absolute sum of all elements present in an array. Below is an example to understand the problem clearly.
Example:
Input: [ -4, -7, 3, 10, 12]
Output: 36
Explanation: Absolute values: 4 + 7 + 3 + 10 + 12 = 36
There are several approaches for finding the absolute sum of array elements using JavaScript which are as follows:
Brute Force Approach
Declare a function which takes an array as its parameter and initialize a variable sum to store the sum of absolute values of array elements. Use a for loop to iterate through each element of the array. Inside the loop, use the Math.abs() function to get the absolute value of each element and add it to the sum variable. Return the final sum.
Example: To demonstration finding absolute sum of array elements using brute force approach.
JavaScript
function absoluteSumIterative(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += Math.abs(arr[i]);
}
return sum;
}
const arr = [-4, -7, 3, 10, 12];
console.log("Absolute sum (iterative) is:", absoluteSumIterative(arr));
OutputAbsolute sum (iterative) is: 36
Time complexity: O(n)
Space complexity: O(1)
Using Array.reduce() method
Declare a function which takes an array as its parameter. Use the reduce() method on the input array arr. Inside the reduce() method, accumulate the sum by adding the absolute value of each element (Math.abs(num)) to the accumulator (sum). Start with an initial value of 0 for the accumulator. Return the final accumulated sum.
Example: Demonstration of finding Absolute sum of array elements using Array.reduce().
JavaScript
function absoluteSumReduce(arr) {
return arr
.reduce((sum, num) => sum + Math.abs(num), 0);
}
const arr = [-4, -7, 3, 10, 12];
console.log("Absolute sum (reduce) is:", absoluteSumReduce(arr));
OutputAbsolute sum (reduce) is: 36
Time complexity: O(n)
Space complexity: O(1)
Using Array.forEach() Method
Declare a function that takes an array as its parameter. Initialize a variable sum to store the sum of the absolute values of the array elements. Use the forEach() method to iterate through each element of the array. Inside the forEach() method, use the Math.abs() function to get the absolute value of each element and add it to the sum variable. Return the final sum.
Example: To demonstrate finding the absolute sum of array elements using the forEach() method.
JavaScript
function absoluteSumForEach(arr) {
let sum = 0;
arr.forEach(num => {
sum += Math.abs(num);
});
return sum;
}
const arr = [-4, -7, 3, 10, 12];
console.log("Absolute sum (forEach) is:", absoluteSumForEach(arr));
OutputAbsolute sum (forEach) is: 36
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
JavaScript Program to Construct an Array from its pair-sum Array The pair-sum array is a unique construction that holds the sum of all potential pairs of elements from the original array. At first glance, it might appear to be challenging, but in this article, we'll Construct an array from its pair-sum array and discover some of its most intriguing uses. What is
4 min read
Javascript Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant
2 min read
Java Program to Compute the Sum of Numbers in a List Using For-Loop Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List
2 min read
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Java Program to Compute the Sum of Numbers in a List Using While-Loop The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion
2 min read