-2

Is it possible to use reduce to find the second largest element in an array? Without using sort methods. Something like this:

Obs: The code below is for finding the largest value. I need to find the second largest value and would like to use reduce(). Without using the sort methods


array1 =  [12, 16, 1, 5]
array2 = [8, 4, 5, 6];

function largestElement(array){

    largestE = array.reduce((acc,currentValue) => currentValue > acc ? currentValue : acc )

    return largestE
}


console.log(largestElement(array1))
console.log(largestElement(array2))

WITHOUT DOING THIS:


 function secondLargest(array){
    
    array.sort((a,b) => a-b)
    return array[array.length-2]

}

  • 3
    Please post code for what you have tried. – mykaf Mar 10 '21 at 21:06
  • 1
    Yes I think it is possible. May you share your effort first? So we can see where you may have struggled? – evolutionxbox Mar 10 '21 at 21:08
  • Does this answer your question? [Sorting Array with JavaScript reduce function](https://stackoverflow.com/questions/50245957/sorting-array-with-javascript-reduce-function) – evolutionxbox Mar 10 '21 at 21:14
  • Please specify if you meant just the sort function, or any sorting function. Also I agree, post some code to show what you have tried. – Espen Mar 10 '21 at 21:45

2 Answers2

0

Yes there are several ways you can do this. Here is one solution:

const array1 = [5, 5, 1, 1, 2, 3, 4];
const array2 = [12,16,1,5];
const reducer = (accumulator, currentValue, idx, a) => {
  let larger = a.filter(n=>n>currentValue) // Fetch larger values than n
                .filter((n,i,a) => a.indexOf(n) === i); // Get Unique values
  if(larger.length === 1){ 
  // if there is only one unique value larger than n, n is your answer.
     return accumulator = currentValue;
  } else {
     return accumulator = accumulator; // else preserve the accumulator value
  }
}
console.log(array1.reduce(reducer));
console.log(array2.reduce(reducer));
Espen
  • 2,456
  • 1
  • 16
  • 25
  • May you consider marking the question as a duplicate of https://stackoverflow.com/questions/50245957/sorting-array-with-javascript-reduce-function? – evolutionxbox Mar 10 '21 at 21:34
  • Well it doesn't directly ask for sorting the array, it asks for getting the second largest value. The other question asks about sorting without using sort. – Espen Mar 10 '21 at 21:36
  • This question is solved by the sorting methods in the linked question. selecting the right item is then trivial – evolutionxbox Mar 10 '21 at 21:41
  • Yes that is one possible solution, but you are still sorting, the question asks not to use sort. I interpreted that as not using any kind of sorting function. – Espen Mar 10 '21 at 21:44
  • Thanks for the feedback. But when I try the array [12, 16, 1, 5], it is returning 0 and I don't know why – leirbagMCA Mar 11 '21 at 12:54
  • Ah I had a bug, the second clause in the if should be accumulator = accumulator, preserving the value if it comes early in the array. I updated my post. – Espen Mar 11 '21 at 14:03
0

I think that the simpler the better, I will just create two variables and compare each element of the array First make two variables outside the loop inside the loop just ask if current number is bigger than larger

  1. if its bigger then assign the value to largest and make the secondLargest with the values of largest 2)if it is not larger than largest but it is smaller than secondLargest than pass that numbe to the secondLargest, please see the code below
    function FindSecondLargest() {
    largest = 0
        secondLargest =0
        for (let i =0 i<array.length, i++)
        {
          if(array[i] >largest)
          {
            secondLargest = largest 
            largest=array[i]
          }else if(array[i] <secondLargest){
            secondLargest=array[i] 
          }
          
        }
      return secondLargest;
    }
Jakub Kurdziel
  • 3,216
  • 2
  • 12
  • 22