Find largest number in an array of integers


Given an array of integers, find the largest number in the array


Assumptions


- array.length >= 1

- Array is unsorted

- Array can contain duplicate numbers


Example


Input: [2,1,9,4,10,3,7,5,6]

Output: 10

Explanation: 10 is the largest number in the given array of numbers


Solution


1. Maintain an int variable - max - to hold the largest number. Initialize to first element of array.

2. Traverse array from second element.
- compare element to max, if max is less than element - then set max to element

3. Return max


Code



Complexity


Time Complexity O(N)

Space Complexity O(1)

 
Subscribe to our Newsletter

 
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example