How to find the largest number using Java

293 Flares Twitter 0 Facebook 3 Google+ 290 LinkedIn 0 Filament.io 293 Flares ×

How do you find the largest largest number of a given series of data. This question is being asked in many interviews and programming evaluation exercises and many developers actually failed to provide a simple answer. Hence thought of writing my point of view on this subject. Many times, developers think in complex ways when there is a simple solution to the problem. It is important remember the principle – “make it simple”.

 

Many people approach this problem as a sorting problem. But we didn’t ask to sort the numbers. The question is just to find the largest / biggest number. The logic using sorting is as follows, sort the series in the descending order and pick the first element. Of course the answer is correct, but you are wasting computing resources and performance. We don’t need to sort to find the bigger number. We just need to use a simple loop.

This approach of using a simple for loop is also efficient as the time complexity of this method is only in the order of N (  Big Oh ( N) ). Typically sorting methodologies are in the order on N^2. Hence this method is simple and efficient.