Shital
Shital

Reputation: 207

Find max and min of a subset of an array

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)

Upvotes: 1

Views: 1246

Answers (1)

Blackwood
Blackwood

Reputation: 4534

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

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

Upvotes: 3

Related Questions