Easy JavaScript sort function solution

  • 0

    Here is the simplest JavaScript solution I could think of. Use the sort function to sort array quickly then simply add up every second index.

    var arrayPairSum = function(nums) {

    nums.sort(function(a, b){return a-b});//sort the array
    var result = 0;
    for(var i = 0; i < nums.length; i=i+2){
        result += nums[i];
    return result;


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.