How to select N unique random values that appear exactly once in an existing list with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class UniqueRandomSelection
{
    /*
        Goal:
        - Select N random values that appear exactly once in the list.
        - The list includes duplicates, but only values with frequency = 1
          are eligible for selection.

        Approach:
        1. Count frequencies using a Dictionary<int,int>.
        2. Collect values that appear exactly once.
        3. Shuffle the unique list.
        4. Take the first N values.
        5. Return them and print them.

        Notes:
        - Uses List<int> for flexibility.
        - Uses Random for randomness.
        - Uses clear, structured functions.
    */

    // Build a frequency map: value -> count
    static Dictionary<int, int> BuildFrequencyMap(List<int> data)
    {
        var freq = new Dictionary<int, int>();

        foreach (int value in data) {
            if (freq.ContainsKey(value))
                freq[value]++;
            else
                freq[value] = 1;
        }

        return freq;
    }

    // Collect values that appear exactly once
    static List<int> CollectGloballyUniqueValues(List<int> data, Dictionary<int, int> freq)
    {
        var unique = new List<int>();

        foreach (int value in data)
        {
            if (freq[value] == 1)
                unique.Add(value);
        }

        return unique;
    }

    // Randomly select N values from the unique list
    static List<int> SelectRandomUnique(List<int> unique, int N)
    {
        if (N > unique.Count)
            N = unique.Count; // clamp

        var temp = new List<int>(unique);
        var rnd = new Random();

        // Fisher–Yates shuffle
        for (int i = temp.Count - 1; i > 0; i--) {
            int j = rnd.Next(i + 1);
            int swap = temp[i];
            temp[i] = temp[j];
            temp[j] = swap;
        }

        return temp.GetRange(0, N);
    }

    // Helper to print a list
    static void PrintList(List<int> lst)
    {
        foreach (int v in lst)
            Console.Write(v + " ");
        Console.WriteLine();
    }

    static void Main()
    {
        // Example list with duplicates
        var data = new List<int> {
            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
        var freq = BuildFrequencyMap(data);

        // Step 2: Collect values that appear exactly once
        var uniqueValues = CollectGloballyUniqueValues(data, freq);

        // Step 3: Choose how many unique random values to select
        int N = 5;

        // Step 4: Select N random unique values
        var 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);
    }
}


/*
run:

Values that appear exactly once:
12 33 59 61 17 74 83 90 1 

Random selection (5 values):
12 17 83 59 74 

*/

 



answered Sep 13 by avibootz

Related questions

...