JavaScript Program of Absolute Sum of Array Elements Last Updated : 09 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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: Table of Content Brute Force Approach Using Array.reduce() methodUsing Array.forEach() MethodBrute 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() methodDeclare 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() MethodDeclare 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) Comment More infoAdvertise with us Next Article JavaScript Program of Absolute Sum of Array Elements bug8wdqo Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program 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 Equilibrium index of an array Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist. The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at high 5 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 Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I 5 min read Calculate the Sum and Average of Elements in an ArrayList in Java A Dynamic and Adaptable method for storing and managing collections of elements is to use ArrayList. Finding the total and average of an ArrayList's items is frequently required when working with numerical data that is stored in the list. In this article, we will see how we can sum and find the aver 3 min read Javascript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts Given an array A[] of length N, where N is an even number, the task is to answer Q independent queries where each query consists of a positive integer K representing the number of circular shifts performed on the array and find the sum of elements by performing Bitwise OR operation on the divided ar 5 min read Subarray whose absolute sum is closest to K Given an array of n non-negative elements and an integer K, the task is to find the contiguous subarray whose sum of elements shows the minimum deviation from K. In other words, find the subarray whose absolute sum is closest to K. Example Input: arr[] = {1, 3, 7, 10}, K = 15Output: 7 10Explanation: 10 min read Like