How to find largest element in an array in easy words?
This article explains a simple coding problem which is to find a largest(or smallest) element among all elements in the given array. Must read if you have started coding or if you are a new computer science student.

Problem Statement : Suppose an array is given to us which does have any number of elements. Now, the aim or problem is to find the largest number among them.
Why do we need to do this? [Application of this problem]
Finding the largest element is very common in daily to daily coding. It could be helpful in a game which shows highest scorer among all the player or in a social website to show most liked post among all yours posts. Anywhere you need to show highest or largest quantity among others this is the application of it.
Hope after reading this article you'll be able to learn this solution. Now, below is the given solution.
Solution's Explanation : In general words we are required to visit every element and need to ask this question whether this number is the bigger than the number known to us till now? If the answer is yes then this current number will be labelled as biggest number. If the answer is no then leave this element and move forward in the array to ask the same question. If we do this thing till the end of array , we will have the biggest number with us.If you get this logic then writing code for this would be a cakewalk. Now, lets understand it with an example then we will look at code snippet of the solution.

As you can see in the above image few numbers are given and they are unsorted. Now, we labelled first element of array which is 2 as largest. Now , we move forward and visits at element 9 , ask the question to our self whether 9 is bigger to the know biggest number which is here 2 and answer is yes because 9 > 2. Therefore, label 9 as largest now by assigning 9 to the largest variable. Repeat this same thing until array is ended and in the end , largest variable will have the biggest/largest number/element in the array.
Now , lets see the code for this logic.

Look at the line number 17 , here we are labelling the first element of array as the biggest number. So, biggest number know to us till now is the first element of array.
Now , at the 20th line we are visiting each element using for loop (in the code for loop condition runs up to n which represents the size of array) and then at line 26th using if condition we are asking whether this current element is bigger than biggest element known to us, if answer is yes then largest will hold that current element and thus we got biggest element till now. Then move forward in the array and do it till end of array.
At last on line 34th we are printing the largest number which is now inside the largest variable.
Let's see the output screen of the code after compilation.

As you can see first line is 6 which shows size of input array. Then next line contains 6 elements value. And then last element outputs 21 which is the largest number among them.
Below paragraph is optional because if you are new to programming then may be you have not studied analysis of algorithm. So, for now you can skip this time complexity paragraph.
Time Complexity: The time complexity of this solution is O(n) where n is size of input array. This is so because we are traversing the whole array only once. There is another solution which takes more time complexity. If you sort the array in increasing order then largest element is at the end. Then you can return that last element. But this solution requires sorting and sorting has time complexity of O(nlogn) therefore, this article solution is best to learn and use.
Moreover, this is the same solution for finding smallest number , the only difference here is to ask a question whether current element is smaller than already known smallest element.
Hope you find this article helpful. Please do like this article which would encourage me to write more articles to make coding problems easy for you.



Comments
There are no comments for this story
Be the first to respond and start the conversation.