WebJan 12, 2024 · int max = int.MinValue; foreach (var type in myList) { if (type > max) { max = type; } } and for min int min = int.MaxValue; foreach (var type in myList) { if (type < min) { min= type; } } Share Improve this answer Follow edited Jan 12, 2024 at 12:41 answered Jan 12, 2024 at 12:35 Moshe D 768 1 4 13 1 I love you so much. Thank you! WebDownload Run Code. Output: Minimum number is -1 Maximum number is 8 3. Using Custom Routine. Finally, we can write a custom routine for finding the minimum and the maximum number of an array. The idea is to traverse the array and keep track of the minimum or maximum value found so far.
C# Max and Min: Get Highest or Lowest Element - Dot Net Perls
WebI'm trying to get the maximum value from an array of integers but if there are two values which are considered maximum, I need to find their index in the array. I.e. If I have the array: {10, 13, 13, 9, 8} I need to find the index of both the 13 values. If I have the array: {10, 13, 12, 9, 8} I need to just return the index of the 13 WebMar 14, 2016 · In order to have the index of max, you should cast IEnumarable to array or list using ToList or ToArray (). var sumList = (from int [,] array in kidsL select (from int item in array select item).Sum ()) .ToList (); var maxSum = sumList.Max (); var maxInd = sumList.IndexOf (maxSum); sumList is a list of ints and contains sums. ips schedule 2021
c# - Write a program to find an index of Max item in an array
WebJul 16, 2014 · I need ideas for a simple, lightweight way to get the max and min values of an array of doubles. The trick is I only need the max and min between two indexes in the array.. The built-in arrayOfDoubles.Max() and arrayOfDoubles.Min() will not work, because they check the entire array.. This code will be running constantly and needs to be … WebNov 29, 2013 · Returns the maximum value in a sequence of values. double [] arr = { 1.5, 10.9, 8.9, 6.5, 10.0 }; Console.WriteLine (arr.Max ()); //10.9 Here a demonstration. Share Improve this answer Follow answered Nov 28, 2013 at 7:30 Soner Gönül 96.4k 102 205 359 Add a comment 1 Assuming your array is named arr WebMar 22, 2024 · Step 1: Create a local variable max and initiate it to arr [0] to store the maximum among the list Step 2: Initiate an integer i = 0 and repeat steps 3 to 5 till i reaches the end of the array. Step 3: Compare arr [i] with max. Step 4: If arr [i] > max, update max = arr [i]. Step 5: Increment i once. ips school 105