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
2009-11-28 06:17:25
Forget my post, I missed the bit where you said 2 arrays. 280's response hit the nail on the head.
Will
2009-11-28 06:29:42
+19
A:
int[] array1 = { 0, 1, 5, 2, 8 };
int[] array2 = { 9, 4 };
int max = array1.Concat(array2).Max();
// max == 9
280Z28
2009-11-28 06:17:55
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
2009-11-28 06:21:31
+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
2009-11-28 06:18:39
+1 for not wasting time and memory concatenating the arrays first
Jason Williams
2009-11-28 06:28:19
@Jason Williams - Concat doesn't allocate an array and so is just as good as this solution.
David B
2009-11-29 01:41:56