/    Sign up×
Community /Pin to ProfileBookmark

Find the Largest and Second Largest number from an array

Hi!
I have a problem. I can’t find the second largest number from an array. This is where I get stack https://codepen.io/raul-rogojan/pen/VwpdpeY?editors=0010 .
I know that there is 1 think that I am missing but I don;t know what

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 07.2021 — Seems to me that one needs two runs of the loop: First find the largest item and then the second largest:
``<i>
</i>var arr = [2000,500,100,2,80,1,50,90,4,50,6,3,30,60,2500,3000,1000,9,40];
var largest = 0;
var secondLargest = 0

for(var i = 0; i &lt; arr.length; i++) {

if(largest &lt; arr[i]) {
largest = arr[i];
}
}
for(var i = 0; i &lt; arr.length; i++) {

if(secondLargest &lt; arr[i] &amp;&amp; arr[i] &lt; largest) {
secondLargest = arr[i];
}

}
console.log('cel mai mare numar este' + largest)
console.log('al doilea cel mai mare numar este' + secondLargest)<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogJun 07.2021 — Maybe just sort it in descending order and then grab the first 2 elements?
[code=javascript]
arr.sort(function(a, b){return b - a});
console.log('cel mai mare numar este' + arr[0]);
console.log('al doilea cel mai mare numar este' + arr[1]);
[/code]
Copy linkTweet thisAlerts:
@KeverJun 07.2021 — Single loop solution:var arr = [2000,500,100,2,80,1,50,90,4,50,6,3,30,60,2500,3000,1000,9,40];
var largest = 0;
var secondLargest = 0

for(var i = 0; i &lt; arr.length; i++) {
if(largest &lt; arr[i]) {
secondLargest = largest;
largest = arr[i];
}
else if(secondLargest &lt; arr[i]) {
secondLargest = arr[i];
}
console.log(arr[i], largest, secondLargest);
}

console.log('cel mai mare numar este ' + largest);
console.log('al doilea cel mai mare numar este ' + secondLargest);
Copy linkTweet thisAlerts:
@codyhillauthorJun 08.2021 — @NogDog#1632654 I was imagining that the arr order need to stay the same
Copy linkTweet thisAlerts:
@codyhillauthorJun 08.2021 — @Kever#1632656 Thx! this was my mistake
Copy linkTweet thisAlerts:
@codyhillauthorJun 08.2021 — @Sempervivum#1632646 this works as well, ty!
Copy linkTweet thisAlerts:
@NogDogJun 08.2021 — > @RaulRogojan#1632677 I was imagining that the arr order need to stay the same

You could then make a copy of the array, and sort the copy. Gets a bit resource intensive for very large arrays, for some undefined value of "very large". 🤷‍♂️
Copy linkTweet thisAlerts:
@elipalmJun 08.2021 — Thanks for sharing.
Copy linkTweet thisAlerts:
@codyhillauthorJun 12.2021 — @NogDog#1632694 Yeah, I did not take that into account, I was trying to give myself a challenge and trying to figure how things work. I noticed that I don't need a second if in the loop and it works like this
``<i>
</i>var arr = [2000,500,100,2,80,1,50,90,4,50,6,3,30,60,2500,55,3000,1000,9,40];
var largest = arr[0];
var secondLargest = arr[0];

for(var i = 0; i &lt; arr.length; i++) {

if(largest &lt; arr[i]) {
secondLargest = largest;
largest = arr[i];
}

console.log('cel mai mare numar este' + largest)
console.log('al doilea cel mai mare numar este' + secondLargest)
}<i>
</i>
`</CODE>

I still do not understand why this works. In my head, <C>
secondLargest</C> should be = 2000 because <C>largest` is = arr[0].
Copy linkTweet thisAlerts:
@NogDogJun 13.2021 — > @RaulRogojan#1632874 I still do not understand why this works.

When 2000 comes in first, secondLargest will be set to 0 (again), and thenlargest will be set to 2000. Nothing will change until you reach 2500, at which point secondLargest will be updated to 2000 and then 2500 assigned to largest. Then nothing will change until 3000 comes along, so 2500 will be moved to secondLargest and largest set to 3000.

However, one possible problem I see would be if there were another 3000 in the array. Since at that point the if would not fire, since it would not be &lt; the already set 3000, it would be skipped. If the desire in the case of a tie would be that both values should be set to 3000, then I think using &lt;= for the comparison would take care of that.
Copy linkTweet thisAlerts:
@KeverJun 13.2021 — It will also give a wrong second largest number, if the largest number comes before the second largest.
Copy linkTweet thisAlerts:
@tracknutJun 13.2021 — Depending on how serious this task is, you might also confirm expected and working solutions when the array looks like:

var arr = [2000,2000,40];

or

var arr = [2000];

or

var arr = [];
[/quote]
Copy linkTweet thisAlerts:
@NogDogJun 13.2021 — > @Kever#1632887 It will also give a wrong second largest number, if the largest number comes before the second largest.

Haven't tested it, but I suspect you're right. Might need an elseif after the if. 🤔
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...