I searched there a few times but came across nothing specifically with this. I have completed this question and here is my code;
int[][] input = {{0}, {0,1}, {4,3,2,1}, {32, 56, 67, 98, 97,8888, 35}};
for(int row = 0; row<input.length; row++) {
Arrays.sort(input[row]);
for(int col = 0; col<input[row].length; col++) {
if (input[row].length < 2) {
System.out.print(0 + " ");
} else {
System.out.print(input[row][1] + " ");
break;
}
}
}
Why I felt the need to post this is to simply inquire, is there a better and more efficient way to answer this question? I simply sorted my arrays and printed the first element which would be the second smallest number. But I am thinking about a better way to complete this question if there were millions of records?
length < 2. --- 2) What's the point of the inner loop? You never usecolfor anything. Also means you don't print anything iflength == 0. --- 3) See duplicate.