I am trying to find largest repeated number in an array and summation of it.
While going through here , I found the solution to find max number. But now I need to count how many max(largest) number is there and store number of maximum number and then summation.
Example:- array- [5,5,7,9,9,9] . So max number is 9 and it is 3 times so , it will store in another array [9,9,9] and total = 27.
I got this to find max number in an array:-
function evaluate() {
const input = prompt("Please enter the array of integers in the form: 1,2,3,1")
.split(',')
.map(nums => nums.trim());
function max(numArray)
{
var nums = numArray.slice();
if (nums.length == 1) { return nums[0]; }
if (parseInt(nums[0]) < parseInt(nums[1])) { nums.splice(0,1); }
else { nums.splice(1,1); }
return max(nums);
}
if (input == "" || input == null) {
document.writeln("Sorry, there is nothing that can be calculated.");
} else {
document.writeln("The largest number is: ");
document.writeln(max(input) + " with a starting input string of: " + input);
}
}
evaluate();
So I want my final output to be shown is 27 from the above example.
it will store in another arrayDo you need another array, or do you really just want the27?27(final output) only.match(/\d+/g). There is also no need for parseInt inparseInt(nums[0]) < parseInt(nums[1]), you can use justnums[0] < nums[1]as relational expressions using<coerce both operands to number.