Return the two highest numbers in an array javascript

Viewed 1418

Write a function called twoHighest that takes an array of numbers as its argument and returns the two highest numbers within the array.

The returned value should be an array in the following format: [secondHighest, highest]

The order of the numbers passed in could be any order.

Do not use the build in sort() method - the tests will fail!

function twoHighest(arr) {
  var highest = 0;
  var secondHighest = 0;

  for (i = 0; i < arr.length; i++) {
    if (arr[i] > highest) {
      highest = arr[i];
    }
  }
  for (i = 0; i < arr.length; i++) {
    if (arr[i] > secondHighest && arr[i] < highest) {
      secondHighest = arr[i];
    }
  }
  return [secondHighest, highest];
}

console.log(twoHighest([1, 2, 10, 8])); // [8, 10]
console.log(twoHighest([6, 1, 9, 10, 4])); // [9,10]
console.log(twoHighest([4, 25, 3, 20, 19, 5])); // [20,25]
console.log(twoHighest([1, 2, 2])); // [2, 2];

This works until the last array [1, 2, 2,]. It is returning [1, 2] rather than [2, 2].

3 Answers

there is a little problem with this piece of code (actually, not only with this, but that's the worst part returning wrong results). Please check my comments inside.

for (i = 0; i < arr.length; i++) {
  if (arr[i] > highest) { // the >= operator must be here
    highest = arr[i]; //you just throw out previous highest, instead of moving it to the second place. Now it is your number 2 
  }
}

So, you should move ex-highest value to the secondHighest place if you found out that value is bigger or equal to the current highest value.

In addition, there is no need to use for() two times. Just use 'else if' condition. The result is going to be like that:

function twoHighest(arr) {
  var highest = 0;
  var secondHighest = 0;

  for (var i = 0; i < arr.length; i++) { // I added i declaration since I did not find where it was declared
    if (arr[i] >= highest) { // >= in here, an explanation is above
      secondHighest = highest; // firstly, move the ex-highest to the second place
      highest = arr[i];
    } else if (arr[i] > secondHighest && arr[i] < highest) {
      secondHighest = arr[i];
    }
  }

  return [secondHighest, highest];
}

And finally, I would like you to take a look at my version of function, may be you will find it useful:

function twoHighest(arr) {
  return arr.reduce((acc, rec) => {
    return rec > acc[1] ? [acc[1], rec] : rec > acc[0] ? [rec, acc[1]] : acc
  },[0,0])
}

Good luck!

I refactored your function into something more reusable give it a go:

const findHighest = (input, depth, result = [], iteration = 1) => {
    if(!input.length) {
        return result;
    }
    let highest = input[0];
    let highestIndex = 0;
    if(input.length === 1) {
        return [...result, highest];
    }
    for(let i = 1; i < input.length; i++) {
        if(input[i] > highest) {
            highest = input[i];
            highestIndex = i;
        }
    }
    input.splice(highestIndex, 1);
    result = [...result, highest];
    if(iteration === depth) {
        return result;
    } else {
        return findHighest(input, depth, result, iteration + 1);
    }
}
console.log(findHighest([1,2,2], 2)); // returns [2,2]

The solution corresponds to my comment on your question: we start with empty result array, and set the iteration to 1(this is the counter which checks if we reached the desired depth in your case its 2), then we basically add highest to the result array and splice the item from the highest index and proceed, you can use this function to find 'N' highest elements in an array

You can simply use if else conditions like this

function twoHighest(arr) {
  let final = [null, null]
  arr.forEach(value => {
    // to update value of firstHighest
    if (value > final[0] && value <= final[1]) {
      final[0] = value
    }
    // when value is greater then secondHighest update firstHighest with value of secondHighest and set secondHighest to value
    else if (value > final[1]) {
      let temp = final[1]
      final[1] = value
      final[0] = temp
    }
  })
  return final
}

console.log(twoHighest([1, 2, 10, 8])); // [8, 10]
console.log(twoHighest([6, 1, 9, 10, 4])); // [9,10]
console.log(twoHighest([4, 25, 3, 20, 19, 5])); // [20,25]
console.log(twoHighest([1,2,2])); // [2,2]

Related