Reputation: 195
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));
Upvotes: 0
Views: 89
Reputation: 5941
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
Upvotes: 0
Reputation: 1136
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:
Something like the following:
function getSecondLargest(nums) {
return Array.from(new Set(nums)).sort().slice(-2)[0]
}
Upvotes: 1
Reputation: 21628
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));
Upvotes: 0
Reputation: 4768
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));
Upvotes: 1