Need to Count the Index

Hey Forum member i have a query where i need all the index position where the value is = 2
example = {1,2,3,2,4,52,6,7,2,8,2,9,2}.

Thanks in advance.

Hi @Darkfighter

Please check the below workflow,

Happy Automation!

1 Like

@Darkfighter


Output

VB :

Dim example As Integer() = {1, 2, 3, 2, 4, 52, 6, 7, 2, 8, 2, 9, 2}

        ' Use LINQ to find all indices where value is 2
        Dim indices = example.Select(Function(value, index) New With {Key .Index = index, Key .Value = value}) _
                              .Where(Function(x) x.Value = 2) _
                              .Select(Function(x) x.Index) _
                              .ToList()

C# :

int[] example = { 1, 2, 3, 2, 4, 52, 6, 7, 2, 8, 2, 9, 2 };

        // Use LINQ to find all indices where value is 2
        var indices = example.Select((value, index) => new { Index = index, Value = value })
                             .Where(x => x.Value == 2)
                             .Select(x => x.Index)
                             .ToList();
1 Like

Hey @prashant1603765 thanks it work. thanks for the help.

1 Like

@Darkfighter

we can condense the code by the following:

Variable:
arrValues = {1,2,3,2,4,52,6,7,2,8,2,9,2}

assign activity:
arrIndexes |int32 Array =

Enumerable.Range(0, arrValues.Length).Where(Function (x) arrValues(x).equals(2)).toArray

if instead a List is needed
assign activity:
IndexList | List(of int32) =

Enumerable.Range(0, arrValues.Length).Where(Function (x) arrValues(x).equals(2)).toList

For LINQ learning have a look here:
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

Thankyou @ppr for this info.

Commented activities will also work fine.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.