views:

169

answers:

4

I have an array of five numbers and an array of 2 numbers. How would I find out the largest number among these 7 numbers? Is there a method that can make things easier?

+1  A: 

If you're using 3.5 you could use Linq:

using System.Linq;
var values = new int[] { 1,2,3,4,5 };
var maxValue = values.Max();
Will
Forget my post, I missed the bit where you said 2 arrays. 280's response hit the nail on the head.
Will
+19  A: 
int[] array1 = { 0, 1, 5, 2, 8 };
int[] array2 = { 9, 4 };

int max = array1.Concat(array2).Max();
// max == 9
280Z28
Very odd, i tried to vote you up. It dropped your vote count to 0 and said vote limit reached. I tried it again and it bumped you up to 2, giving the same message! :S
RCIX
+3  A: 

Straightforward way:

Math.Max(Math.Max(a,b), c)//on and on for the number of numbers you have

using LINQ:

int[] arr1;
int[] arr2;
int highest = (from number in new List<int>(arr1).AddRange(arr2)
               orderby number descending
               select number).First();
RCIX
+10  A: 

You can try

decimal max = Math.Max(arr1.Max(), arr2.Max());
astander
+1 for not wasting time and memory concatenating the arrays first
Jason Williams
@Jason Williams - Concat doesn't allocate an array and so is just as good as this solution.
David B