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
'