How to convert a numeric array into a range?

Mansour_Dalir 2,036 Reputation points
2023-05-18T10:07:24.9766667+00:00

myArray: 1,2,3,5,6,8,9,7,10,20,21,23,22 at 10000 integer [but Unique]...

need convert to

1-3 and 5-10 and 20-22 ...

I have an array like this: myArray

I want it to be converted to range with the least iteration loop (for)..I can't write code in parallel..maybe parallel is the answer.

With the best technique for high speed

When it is in the filter is like this, the speed is very low.

MyDataTable.DefaultView.RowFilter = "[ID] in(1,2,4,5,6,8,7,9,10)" 'at 10000

And when the filter is like this, the speed is higher:

MyDataTable.DefaultView.RowFilter = "[ID] >=1AND [ID] <=4 OR [ID] >=6 AND [ID] <=12"

Thank.

tags: vb.net-vs2019-dataTable-DataGridView-Filter

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,891 questions
{count} votes

Accepted answer
  1. Viorel 122.3K Reputation points
    2023-05-18T17:28:59.25+00:00

    Check this example:

    Dim myArray As Integer() = {1, 2, 3, 5, 6, 8, 9, 7, 10, 20, 21, 23, 22}
    
    Dim result As Integer()() = myArray _
        .OrderBy(Function(x) x) _
        .Select(Function(x, i) New With {x, i, .d = x - i}) _
        .GroupBy(Function(t) t.d) _
        .Select(Function(g) New Integer() {g.Min(Function(t) t.x), g.Max(Function(t) t.x)}) _
        .ToArray
    

    It returns a bidimensional array; each row contains the range.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.