How to create a common sorted unique list from 3 integer lists in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Module MergeUniqueSorted

    '
    '   Function: MergeLists
    '   Purpose:  Combine three integer lists into a single list.
    '
    Function MergeLists(lstA As List(Of Integer),
                        lstB As List(Of Integer),
                        lstC As List(Of Integer)) As List(Of Integer)

        Dim lstMerged As New List(Of Integer)
        lstMerged.AddRange(lstA)
        lstMerged.AddRange(lstB)
        lstMerged.AddRange(lstC)

        Return lstMerged
    End Function

    '
    '   Function: UniqueSorted
    '   Purpose:  Convert a list into a sorted list with unique elements.
    '             Uses HashSet to remove duplicates, then sorts the result.
    '
    Function UniqueSorted(lst As List(Of Integer)) As List(Of Integer)
        Dim setUnique As New HashSet(Of Integer)(lst)   ' unique values
        Dim lstResult As New List(Of Integer)(setUnique)

        lstResult.Sort()                                ' sort ascending

        Return lstResult
    End Function

    Sub Main()

		' Input lists
		Dim lst1 As New List(Of Integer) From {5, 1, 14, 3, 8, 9, 1, 1, 7}
		Dim lst2 As New List(Of Integer) From {3, 5, 7, 2, 3}
		Dim lst3 As New List(Of Integer) From {2, 9, 8}

        ' Step 1: Merge all lists
        Dim lstMerged As List(Of Integer) = MergeLists(lst1, lst2, lst3)

        ' Step 2: Create sorted unique list
        Dim lstResult As List(Of Integer) = UniqueSorted(lstMerged)

        ' Step 3: Print result
        Console.Write("Sorted unique array: ")
        For Each x In lstResult
            Console.Write(x & " ")
        Next

    End Sub

End Module



' run:
' 
' Sorted unique array: 1 2 3 5 7 8 9 14 
'

 



answered May 6 by avibootz

Related questions

...