Reputation: 25
So I am doing a HackerRank JavaScript Challenge(Array Section), basically I need to find the second largest number in an array. I was getting close and looked in the discussion to help get my answer but I'm confused on how it works. The nums array is an array of numbers of n length, which allows duplicates. n can be 1<=n<10, nums can be 1<=nums<=100. The code I have is here :
function getSecondLargest(nums)
{
var largestNum = nums[0];
var secondLargest = nums[0];
for (let i = 0; i < nums.length; i++)
{
if (nums[i] > largestNum)
{
secondLargest = largestNum;
largestNum = nums[i];
continue;
}
if ((nums[i] > secondLargest) && (nums[i] < largestNum))
{
secondLargest = nums[i];
}
}
return secondLargest;
So I know by the final loop iteration that secondLargest = 9.
My issue is I believe LargestNum = 10 by the end of the final loop, first if block. So if LargestNum = 10, How does that fulfill this if statement requirement.
num[9] = 10 (I think)
largestNum = 10 (I think)
secondlargest = 9 ( I think)
if ((nums[i] > secondLargest) && (nums[i] < largestNum))
10 is not less then 10
The answer I get is correct, secondLargest = 9;
I'm just not certain how the code arrived there.
Upvotes: 0
Views: 92
Reputation: 789
If you are using ES6, a nice solution is:
const nums = [7, 4, 9, 8, 4, 4, 2, 1, 9, 5, 2, 5, 3, 1, 7];
const numsUnique = [...new Set(nums)] // Remove duplicates
const sorted = numsUnique.sort((a, b) => a - b); // Order by ascending
const secondLargest = sorted[sorted.length - 2]; // Get the second last element
Upvotes: 1
Reputation: 2148
We use set to remove duplicates data = [...new Set(data)];
, quick and easy.
function secondHighest(data) {
data = [...new Set(data)]; // Remove dupicates
// New data after removing duplicates
console.log('Data to test', data)
// Max value placeholder
let max = data[0];
// Second largest placeholder
let second_biggest = data[0];
// For loop for our data length
for (let i = 0; i < data.length; i++){
// If current item in data is > max
if(data[i] > max){
// Second largest now equals max
second_biggest = max;
// And max = current data item
max = data[i];
}
// Else if data item > second largest and data item not equal to max
else if (data[i] > second_biggest && data[i]!== max) {
second_biggest = data[i];
}
}
return second_biggest;
}
console.log(secondHighest([1, 2, 3, 4, 50, 60, 60, 7, 8, 9, 99]));
Upvotes: 2
Reputation: 10627
If I was you, I would make a max function to get the maximum number. Then I would filter out ever number that is not equal to the max, then run max again on the remaining array.
function max(array){ // JavaScript already has Math.max, but it takes arguments so we apply
return Math.max.apply(Math, array);
}
function secondMax(array){
var a = array.slice(), m = max(array); // .slice() so original array is not affected
a = a.filter(function(v){
return v !== m;
});
return max(a);
}
console.log(secondMax([1, 77, 25, 72, 77, 23, 72, 21]));
Upvotes: 0
Reputation: 55663
With duplicates:
const secondLargest = nums => {
const desc = nums.sort((a, b) => a - b).reverse();
let i = 0;
let n = desc[i];
while (n >= desc[0]) {
i++;
n = desc[i];
}
return n;
}
console.log(secondLargest([1, 77, 27, 54, 63, 77, 19, 72, 100, 200, 200, 79]));
EDIT:
Piggy-backing off another answer - just use Set
and you can shorten it to:
const secondLargest = nums => [...new Set(nums)].sort((a, b) => a - b).reverse()[1]
console.log(secondLargest([1, 77, 27, 54, 63, 77, 19, 72, 100, 200, 200, 79]));
EDIT #2:
Curious as to what the restrictions are on nums
- is it possible for nums
to be [100,100]
for instance? Or what about [100]
?
Upvotes: 2