Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,226 questions

56,128 answers

573 users

How to select N unique random values that appear exactly once in an existing list with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Module UniqueRandomSelection

    '---------------------------------------------------------------
    ' Build a frequency map: value -> count
    '---------------------------------------------------------------
    Function BuildFrequencyMap(data As List(Of Integer)) As Dictionary(Of Integer, Integer)
        Dim freq As New Dictionary(Of Integer, Integer)()

        ' Count how many times each value appears in the list
        For Each value In data
            If freq.ContainsKey(value) Then
                freq(value) += 1
            Else
                freq(value) = 1
            End If
        Next

        Return freq
    End Function

    '---------------------------------------------------------------
    ' Collect values that appear exactly once in the entire list
    '---------------------------------------------------------------
    Function CollectGloballyUniqueValues(data As List(Of Integer),
                                         freq As Dictionary(Of Integer, Integer)) As List(Of Integer)

        Dim unique As New List(Of Integer)()

        ' Only values with frequency = 1 are eligible
        For Each value In data
            If freq(value) = 1 Then
                unique.Add(value)
            End If
        Next

        Return unique
    End Function

    '---------------------------------------------------------------
    ' Randomly select N values from the unique list
    '---------------------------------------------------------------
    Function SelectRandomUnique(unique As List(Of Integer), N As Integer) As List(Of Integer)
        If N > unique.Count Then
            N = unique.Count   ' clamp to available unique values
        End If

        ' Shuffle the list to randomize order
        Dim temp As New List(Of Integer)(unique)
        Dim rnd As New Random()

        ' Fisher–Yates shuffle
        For i As Integer = temp.Count - 1 To 1 Step -1
            Dim j As Integer = rnd.Next(i + 1)
            Dim swap As Integer = temp(i)
            temp(i) = temp(j)
            temp(j) = swap
        Next

        ' Take first N shuffled elements
        Return temp.GetRange(0, N)
    End Function

    '---------------------------------------------------------------
    ' Helper to print a list
    '---------------------------------------------------------------
    Sub PrintList(lst As List(Of Integer))
        For Each v In lst
            Console.Write(v & " ")
        Next
        Console.WriteLine()
    End Sub

    '---------------------------------------------------------------
    ' Main program
    '---------------------------------------------------------------
    Sub Main()

        ' Example list with duplicates
        Dim data As New List(Of Integer) From {
            5, 12, 5, 19, 5, 33, 19, 5, 8, 8, 8, 59, 61, 17, 3, 5, 3, 74, 83, 90, 3, 1
        }

        ' Step 1: Build frequency map
        Dim freq = BuildFrequencyMap(data)

        ' Step 2: Collect values that appear exactly once
        Dim uniqueValues = CollectGloballyUniqueValues(data, freq)

        ' Step 3: Choose how many unique random values to select
        Dim N As Integer = 5

        ' Step 4: Select N random unique values
        Dim randomSelection = SelectRandomUnique(uniqueValues, N)

        ' Step 5: Print results
        Console.WriteLine("Values that appear exactly once:")
        PrintList(uniqueValues)

        Console.WriteLine()
        Console.WriteLine("Random selection (" & N & " values):")
        PrintList(randomSelection)

    End Sub

End Module


' run:
'
' Values that appear exactly once:
' 12 33 59 61 17 74 83 90 1 
' 
' Random selection (5 values):
' 90 61 74 83 1 
' 

 



answered Sep 13 by avibootz

Related questions

...