查找数组子集的最大值和最小值

时间:2022-07-28 20:12:10

I wanted to find the maximum and minimum from part of array. I know I can get the required part of the array into another array by copying it but just want to know if it is possible without copying the array as I have to go through a loop for different sub array's

我想从数组的一部分找到最大值和最小值。我知道我可以通过复制将数组所需的部分放到另一个数组中,但只是想知道是否可以不复制数组,因为我必须通过不同子数组的循环

For example:

arr1 = {1,2,3,4,5,6,7,8,9,10}

Now I want to find the min/max of the subarray from 1 to 4 (if possible without copying subarray)

现在我想找到从1到4的子阵列的最小值/最大值(如果可能的话,不复制子阵列)

1 个解决方案

#1


3  

You can use the Skip and Take methods to select the subset of the array before calling the Max or Min methods.

在调用Max或Min方法之前,可以使用Skip和Take方法选择数组的子集。

For example, to get the maximum number from the first four elements of the array

例如,从数组的前四个元素中获取最大数量

Dim arr1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim arrMax As Integer = arr1.Take(4).Max

Or if you want to skip the first element and get the maximum number of the next four elements of the array

或者,如果要跳过第一个元素并获取该数组的下四个元素的最大数量

Dim arr1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim arrMax As Integer = arr1.Skip(1).Take(4).Max

#1


3  

You can use the Skip and Take methods to select the subset of the array before calling the Max or Min methods.

在调用Max或Min方法之前,可以使用Skip和Take方法选择数组的子集。

For example, to get the maximum number from the first four elements of the array

例如,从数组的前四个元素中获取最大数量

Dim arr1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim arrMax As Integer = arr1.Take(4).Max

Or if you want to skip the first element and get the maximum number of the next four elements of the array

或者,如果要跳过第一个元素并获取该数组的下四个元素的最大数量

Dim arr1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim arrMax As Integer = arr1.Skip(1).Take(4).Max