0

# What is the code to write a program in order to find the second least number ?

I want a program to find the 2nd least number of an unsorted array

4 Answers

+ 4

Logic : Sort the array in ascending order and the 2nd element is what you want.

+ 3

Krishna's Method works, but it's unnecessarily inefficient if you don't need the sorted list later on.
Better just go through the array and keep/update temp values for the smallest and second smallest number you encountered.
Runs in linear time compared to the O(n*log(n)) of a sorting algorithm and in practice the difference in speed will be larger, than Landau notation suggests.

0

Ok, for School assignment?

0

No, its for my own knowledge