How to get the index of duplicate elements along with the range of empty elements after it. LINQ

Mansour_Dalir 1,651 Reputation points
2024-05-04T15:38:26.2533333+00:00

hi

Dim ComplexArray As String() = Split("A   C  A     B   C   A B  A  B    C")
  
Dim Arr3D = {({"A", ({"0-2", "5-9", "16-16", "19-20"})}), ({"B", ({"10-12", "17-18", "21-24"})}), ({"C", ({"3-4", "13-15", "25-25"})})}

When we copy vertically merged cells in Excel, such an array is obtained.(ComplexArray) . Trying to Get The number of duplicates in the column where the cells are randomly merged, then the difference between the rows of the duplicates and z-a Descending of the duplicate cell

If possible, check with a text file.Data.txt. Thank you all

Note: It can be similar to this answer but with some changes (This Question)

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,849 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,676 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,599 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,921 Reputation points Microsoft Vendor
    2024-05-06T08:06:23.15+00:00

    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.