Let\'s say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2]
[2,3,3,4,2,2,5,6,7,2]
What is the best way to find the minimum or maximum value in that Array?
Right now,
If you want to find both the min and max at the same time, the loop can be modified as follows:
int min = int.maxValue; int max = int.minValue; foreach num in someArray { if(num < min) min = num; if(num > max) max = num; }
This should get achieve O(n) timing.