How to select N unique random values that appear exactly once in an existing array with JavaScript

1 Answer

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

    Approach:
    1. Count frequencies using a Map.
    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 modern JavaScript (ES6+).
    - Uses expressive, structured functions.
*/

//---------------------------------------------------------------
// Build a frequency map: value -> count
//---------------------------------------------------------------
function buildFrequencyMap(data) {
    const freq = new Map();

    for (const value of data) {
        freq.set(value, (freq.get(value) || 0) + 1);
    }

    return freq;
}

//---------------------------------------------------------------
// Collect values that appear exactly once
//---------------------------------------------------------------
function collectGloballyUniqueValues(data, freq) {
    return data.filter(value => freq.get(value) === 1);
}

//---------------------------------------------------------------
// Randomly select N values from the unique list
//---------------------------------------------------------------
function selectRandomUnique(unique, N) {
    if (N > unique.length) {
        N = unique.length; // clamp
    }

    // Shuffle using Fisher–Yates
    const temp = [...unique];
    for (let i = temp.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [temp[i], temp[j]] = [temp[j], temp[i]];
    }

    return temp.slice(0, N);
}

//---------------------------------------------------------------
// Print helper
//---------------------------------------------------------------
function printList(list) {
    console.log(list.join(" "));
}

//---------------------------------------------------------------
// Main program
//---------------------------------------------------------------
(function main() {

    // Example array with duplicates
    const data = [
        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
    const freq = buildFrequencyMap(data);

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

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

    // Step 4: Select N random unique values
    const randomSelection = selectRandomUnique(uniqueValues, N);

    // Step 5: Print results
    console.log("Values that appear exactly once:");
    printList(uniqueValues);

    console.log("\nRandom 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 90 33 1 17

*/

 



answered Sep 13 by avibootz

Related questions

...