Hi @Mansour_Dalir ,
Please check the follow code.
Dim ComplexArray As String() = Split("A C A B C A B A B C")
Dim resultArray As New List(Of Object())()
Dim indices As New Dictionary(Of String, List(Of String))()
Dim lastIndex As Integer = -1
Dim lastElement As String = ""
For i As Integer = 0 To ComplexArray.Length - 1
Dim element As String = ComplexArray(i).Trim()
If Not String.IsNullOrEmpty(element) Then
If String.IsNullOrEmpty(lastElement) Then
Dim rangeList1 As New List(Of String)()
rangeList1.Add(i.ToString())
indices.Add(element, rangeList1)
Else
If Not indices.ContainsKey(element) Then
Dim rangeList1 As New List(Of String)()
rangeList1.Add(i.ToString())
indices.Add(element, rangeList1)
Dim rangeList2 As List(Of String) = indices(lastElement)
rangeList2(rangeList2.Count - 1) = rangeList2(rangeList2.Count - 1) & "-" & (i - 1)
Else
Dim rangeList1 As List(Of String) = indices(element)
rangeList1.Add((i).ToString())
Dim rangeList2 As List(Of String) = indices(lastElement)
rangeList2(rangeList2.Count - 1) = rangeList2(rangeList2.Count - 1) & "-" & (i - 1)
End If
End If
lastElement = element
End If
Next
Dim rangeList3 As List(Of String) = indices(lastElement)
rangeList3(rangeList3.Count - 1) = rangeList3(rangeList3.Count - 1) & "-" & (ComplexArray.Length - 1)
For Each pair In indices
Dim element As String = pair.Key
Dim ranges As List(Of String) = pair.Value
Dim result As Object() = {element, ranges.ToArray()}
resultArray.Add(result)
Next
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.