0

This code is supposed to return the second largest number in the array. However I notice that this code does not work if there are duplicate elements in the array. It returns the largest number as the second largest number when I set the array equal to [2, 3, 6, 6, 5]. It is supposed to return 5 as the second largest and the 6 is the first largest.

I added portion of code in the else if to see if I can delete a duplicate but it didn't work it is still returning 6 instead of 5.

const nums = [2, 3, 6, 6, 5];

function getSecondLargest(nums) {
var firstLargest = nums[0];
var secondLargest = nums[0];

for(var i = 1; i < nums.length; i++){
    if (nums[i] > secondLargest){
        secondLargest = firstLargest;
        firstLargest = nums[i];
     }

    else if ((secondLargest === nums[i]) || (firstLargest === nums[i])){
        delete nums[I];

     }
    else{
        nums[i] = secondLargest;    
    }

}
return secondLargest;

} console.log(getSecondLargest(nums));

3
  • Your definition of second largest is rather unusual. Commented Feb 6, 2019 at 23:42
  • So if a dupe than do not add it... Commented Feb 7, 2019 at 0:10
  • What would be the expected return value for an Array that is empty nums=[], has only one value nums=[5] or contains only one kind of number nums=[5,5,5,5,5]? Commented Feb 7, 2019 at 5:44

4 Answers 4

1

Just need to test for equals when evaluating secondLargest.

const nums = [2, 3, 6, 6, 5];

function getSecondLargest(nums) {
  var largest = 0;
  var secondLargest = 0;

  nums.forEach(n => {
    if (n > largest ) {
      secondLargest = largest;
      largest = n;
      
    } else if ( n > secondLargest && n !== largest ){
      secondLargest = n;
    }
  });

  return secondLargest;

}

console.log(getSecondLargest(nums));

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

Comments

1

If you want to deduplicate items in an array, an easy way is to convert it into a Set: new Set(array), and then, if required, create a new array from it: Array.from(set). Putting everything together: Array.from(new Set(array)).

Now, another way of getting the second largest number from a given array could be to:

  1. remove any duplicates;
  2. sort the array;
  3. get the second last value;

Something like the following:

function getSecondLargest(nums) {
  return Array.from(new Set(nums)).sort().slice(-2)[0]
}

Comments

0

I have a function I use to get the distinct items in an array, if you then sort this distinct list the second largest will be second item in the list.

const nums = [2, 3, 6, 6, 5];

const distinct = (array) =>
  array
    ? array.reduce((results, item) => {
        if (!results.some((i) => i === item)) {
          results.push(item);
        }
        return results;
      }, [])
    : array;

const getSecondLargest = array => array && array.length >= 2 ? distinct(array).sort((a,b) => b - a)[1] : undefined;

console.log(getSecondLargest(nums));

Comments

0

You can first filter the array so you are left with an array containing only distinct values. Next you can the filtered array, sort it, and return the number at the second index of the sorted array:

const a = [2, 3, 6, 6, 5];
const b = [5, 4, 2, 1, 3];
const c = [10, 12, 11, 9, 10];
const d = [8, 14, 7, 2, 1, 11, 18, 7, 4];

function getSecondLargest(arr) {
  const filtered = arr.reduce((accum, el) => {
    if (accum.indexOf(el) === -1) {
      accum.push(el);
    }
    return accum;
  }, []);
  const sorted = filtered.sort((a, b) => b - a);
  return sorted[1];
}

console.log(getSecondLargest(a)); //should return 5
console.log(getSecondLargest(b)); //should return 4
console.log(getSecondLargest(c)); //should return 11
console.log(getSecondLargest(d)); //should return 14

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.