Harder Array Loop Practice
Optional Node
Collapse Content
Show Content
Array Loop Practice asked for the smallest number in an array, but can solve a harder question?
Challenge
You are given ar
, an array of numbers, and need to print the second smallest number in the array. You can modify your solution to Array Loop Practice to find the desired number. Different solutions are possible...
Challenge
Print the second smallest number in an array.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Keyana K
Nov 4, 8:18 PMIts reading my < symbol as a > symbol
Tyler Pond
Nov 22, 6:02 PMIf anybody needs help, take a look at this solution using the bubble sort algorithm.
calo
Jan 13, 8:02 PMhmmm cant help it, no idea ..is there easier way than "this solution"?
Learneroo
Jan 13, 9:08 PM@calo, this problem can be solved much in a much simpler manner. See the hint that I added to the problem.
Victoria Holland
Jan 14, 12:45 PMI got there in the end! This is the link to my answer: http://www.learneroo.com/user_answers/0675578220
Learneroo
Jan 14, 3:57 PM@Victoria, well done! When you learn how to create your own methods, you'll be able to improve it further to avoid code duplication.
Ksenia Sukmanskaya
Jan 23, 7:46 AMI think that solutions given on links above are far from perfection, since they change the data of the array
Learneroo
Jan 23, 9:10 AM@Ksenia, that's OK in this challenge. Another approach would be to just track the 2 smallest numbers in the array as you go through it..
Tiago Sirious
Jan 28, 8:46 AMIf you use the way shown in the Hint, be sure to declare your "secondsmallest" variable AFTER changing the smaller variable to a bigger number. Otherwise inputs 1 and 3 wont work!
Btw, thanks to @Victoria Holland, I understood the hint trough her example.
Robin Elliott
Feb 3, 6:41 PMI get the three numerical outputs green but it still says incorrect ("Your Output" is in red). Is it running some other checks that aren't being announced?
Learneroo
Feb 3, 6:50 PM@Robin, yes, it often runs on other cases and only shows the overall result for them.
Robin Elliott
Feb 3, 10:26 PMI finally got it! Though I feel like I cheated a bit by setting my 2nd-smallest variable to an unreasonably high number to begin with, rather than setting it to a value drawn from the array itself. It's here: http://www.learneroo.com/user_answers/8850767557
(P.S. Please tell me that Java has a way to automatically return the second-smallest number from an array. I'd hate to actually have to use this sort of stuff in production code.)
Learneroo
Feb 3, 11:32 PMSetting it to a large number is fine, though you could have also selected a number from the array. Java doesn't have any built-in way to do it, though maybe it could be done a little cleaner.
See also the featured submissions for this challenge.
Cliff Karlsson
Feb 18, 4:37 AMwhy does not this work?
int smallest = ar[0];
int secondSmallest = ar[0];
Dino Šišić
Feb 20, 8:32 AMHere is the solution for the unpatient ones:
static void doStuff(int[] ar){
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;
// Loop over the array
}
David
Mar 2, 12:53 AMTook me all day today and i think a little of yesterday to figure this exercise out.
//your code here
//Modified the code to comply this exercise with the input provided
Never mind to answer i guess after further lessons i'll prob get to know, this is just for those people out there that go through this exercise without landing on the dot and don't want to "cheat", the exercise is made to comply with scenario provided, meaning input given at the botton, period!
Anyway gotta thank Learneroo for helping noobs like me : )
Jordan Bouvier
Apr 3, 4:59 PMHere's an example without "cheating" by having to pick an absurdly large number:
http://www.learneroo.com/user_answers/6675108810
許友誠
May 5, 12:02 PMJohn Slow
Jun 23, 2:53 PM@Dino Šišić : there is a better solution that doesnt require using integer.Max value (using this would make the problem trivial). Also, there must be no repeating value in the array
Julian
Jul 4, 7:39 AMloupams
Jul 16, 2:22 AMAnother solution:
http://www.learneroo.com/user_answers/1556388287
Vikesh Kumar Sharma
Jul 30, 10:52 AMit might not be the best solution
int small = ar[0];
int large = ar[0];
int index = 0;
int repeat = 0;
Macro
Aug 18, 10:39 PMJake Levenson
Sep 6, 2:32 PMKeep the previous code to find the smallest number, and after that code, add the exact same thing over again with a second condition stating that the cell cannot be equal to the smallest number.
Corey Sheely
Sep 30, 1:45 AMCan anyone tell me why this won't work?
}
Learneroo
Sep 30, 10:14 AM@corey
ar[i]=smallest;
will modify the value in the array, is that really what you want to do?Gaetano
Oct 25, 12:25 AMLiterally tried this for 5 hours without trying to use Google as help, What am i doing wrong here?
import java.util.*;
public class Main{
static void doStuff(int[] ar){
Learneroo
Oct 25, 7:46 PM@Gaetano, step through your code with the examples that are wrong to spot the error. For example, what happens when your code runs on the first case? Consider changing the smallest numbers so it doesn't cause trouble the second time...
thales
Jul 13, 8:13 AMIt took me a while (from int i = one a clock; i= two o clock; i++) :-) but i think i found it.
Here is my answer.
my code