0

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.

3
  • it will store in another array Do you need another array, or do you really just want the 27? Commented May 21, 2018 at 9:03
  • @CertainPerformance As of now 27 (final output) only. Commented May 21, 2018 at 9:04
  • 1
    Instead of using split+map+trim you could just use match(/\d+/g). There is also no need for parseInt in parseInt(nums[0]) < parseInt(nums[1]), you can use just nums[0] < nums[1] as relational expressions using < coerce both operands to number. Commented May 21, 2018 at 9:09

5 Answers 5

4

This returns an object with the specified outputs {max: 9, items: [9,9,9], sum: 27}.

function maxElementSum(arr) {
    var max = Math.max(...arr)
    var count = arr.filter(el => el == max).length
    return {max: max, items: Array(count).fill(max), sum: max * count}
}

console.log(maxElementSum([5,5,7,9,9,9]))

Sign up to request clarification or add additional context in comments.

Comments

1

Use Math.max to get the largest value and then use the filter & reduce to sum the value

// find the largest number
var _gt = Math.max(...[5, 5, 7, 9, 9, 9])
// then filter the array and get the largest values
// and use reduce to sum the numbers
var arr = [5, 5, 7, 9, 9, 9].filter(function(item) {
  return _gt === item
}).reduce(function(acc, curr) {
  acc += curr;
  return acc;

}, 0);
console.log(arr)

Comments

1

You could count the values, reduce the max count and return the multiplication of value and count.

var array = [5, 5, 7, 9, 9, 9],
    max = Object
        .entries(
            array.reduce((r, v) => (r[v] = (r[v] || 0) + 1, r), Object.create(null))
        )
        .reduce((a, b) => a[1] > b[1] ? a : b)
        .reduce((a, b) => a * b);
    
console.log(max);

Comments

1

If you start with an array, then you only need reduce to both find the maximum value and to return the sum, e.g.

var nums = [5,5,7,9,9,9],
    max  = -Infinity,
    result = nums.reduce((acc, num) => num > max? acc = max = num : num == max? acc += num : acc, 0);

console.log('Result: ' + result);

Comments

1

You can do it easily just using one forEach loop in O(n) time,

var array = [5, 5, 7, 9, 9, 9];
var max = array[0], total = 0;
array.forEach((a)=>{
  if(a==max){
    total+=max;
  }
  else if(a>max){
    max = total = a;
  }
});
console.log("total:"+total);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.