How to convert a numeric array into a range?

Mansour_Dalir 1,716 Reputation points

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"



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

Accepted answer
  1. Viorel 114.4K Reputation points

    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)}) _

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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful