Reputation: 445
I have an array where I am looping over, finding the largest number and then taking the sum total of all other numbers in the array and seeing if it equals the largest number. I did this using two for loops. I am trying to figure out how to do it with just one loop and I cant seem to figure it out. Would you lend some advice please.
var myArray = [4, 6, 24, 10, 1, 3];
var arrayAddition = function (arr) {
var largestNumber = arr[0];
var sumTotal = 0;
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] > largestNumber) {
largestNumber = arr[i];
}
}
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] != largestNumber) {
sumTotal += arr[i];
}
}
if (largestNumber === sumTotal) {
return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber;
} else {
return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber;
}
};
Upvotes: 0
Views: 761
Reputation: 3561
You could sum all the numbers and then subtract the largestOne in the end
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] > largestNumber) {
largestNumber = arr[i];
}
sumTotal += arr[i];
}
// now because the sumTotal includes the largestNumberAlso
// to get the same result as in your code, you have to
sumTotal -= largestNumber
var myArray = [4, 6, 24, 10, 1, 3];
var arrayAddition = function (arr) {
var largestNumber = arr[0];
var sumTotal = 0;
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] > largestNumber) {
largestNumber = arr[i];
}
sumTotal += arr[i];
}
sumTotal -= largestNumber;
if (largestNumber === sumTotal) {
return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber;
} else {
return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber;
}
};
There is one more improvement to make your code slightly faster
Instead of doing the loop like this
for( var i = 0; i < arr.length; i += 1 ) { ... }
You can write it like this
for( var i = 0, len = arr.length; i < len; i++ ) { ... }
And that way you can save some processor cycles because you don't need to lookup the length attribute every time
var myArray = [4, 6, 24, 10, 1, 3];
var arrayAddition = function (arr) {
var largestNumber = arr[0]; // that could satisfy the equation
var sumTotal = 0;
for( var i = 0, len = arr.length; i < len; i++ )
// change it only if it is bigger then the current sum,
// this way the largest number may be incorect if it is
// smaller then the sum, but we don't care for that
// on the other hand we can save some cpu cycles by not
// doing a 'largestNumber = arr[i]' operation
// if we don't really need to
if (arr[i] > sumTotal) {
largestNumber = arr[i];
}
sumTotal += arr[i];
}
sumTotal -= largestNumber;
if (largestNumber === sumTotal) {
return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber;
} else {
return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber;
}
};
Upvotes: 4
Reputation: 63587
I think Igor has nailed it, but I thought I'd let you know of some other interesting ways of getting the largest number in an array, and also filtering and reducing arrays.
You can use Math.max.apply
to get the largest number in a array.
var max = Math.max.apply(null, arr);
Likewise, you can use Math.min.apply
to get the smallest number in a array.
var min = Math.min.apply(null, arr);
This code (IE9 and above) returns the array without the maximum number in it using filter
, and then sums the numbers that remain using reduce
.
var sum = arr.filter(function (el) {
return el != max;
}).reduce(function (p, c) {
return p + c;
});
This uses more than one loop obviously, but you might find the information useful.
Upvotes: 1
Reputation: 2944
Calculate the sum of all of the numbers in the first loop, as well as finding the largest. The sum of every other number is then just the sum of all take the largest number. This can be done as so:
var myArray = [4, 6, 24, 10, 1, 3];
var arrayAddition = function (arr) {
var largestNumber = arr[0];
var sumTotal = 0;
for (var i = 0; i < arr.length; i += 1) {
sumTotal += arr[i];
if (arr[i] > largestNumber) {
largestNumber = arr[i];
}
}
sumTotal -= largestNumber;
if (largestNumber === sumTotal) {
return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber;
} else {
return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber;
}
};
Importantly, these methods differ on the case when there is more than one of the largest number - in your code, all numbers of the same magnitude as the largest number are ignored whereas in the code above only the one instance is ignored. A fix for this (Assuming you meant for this behaviour to be present) would be something like changing the loop to be:
var largestNumber = arr[0];
var largeCount = 0;//We haven't actually seen any yet.
var sumTotal = 0;
for (var i = 0; i < arr.length; i += 1) {
sumTotal += arr[i];
if (arr[i] > largestNumber) {
largestNumber = arr[i];
largeCount = 1; //We have just one of these now
} else if (arr[i] == largestNumber) {
largeCount++; //Another big one is found!
}
}
sumTotal -= largeCount * largestNumber; //Take all largest numbers out.
Upvotes: 2
Reputation: 31930
So what I would do is sort the array numerically. Then sum all the values except for the last one.
var myArray = [4, 6, 24, 10, 1, 3];
myArray.sort(function(a,b){return a-b});
var arrayAddition = function (arr) {
var largestNumber = arr[arr.length-1]; // get last element; it's the biggest
var sumTotal = 0;
for (var i = 0; i < arr.length-1; i += 1) {
sumTotal += arr[i];
}
if (largestNumber === sumTotal) {
return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber;
} else {
return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber;
}
};
Upvotes: -2