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

1 Answer

0 votes
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
    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 HashMap.
    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<Integer> for flexibility.
    - Uses Collections.shuffle for randomness.
    - Uses clear, structured functions.
*/

public class UniqueRandomSelection {

    // Build a frequency map: value -> count
    static Map<Integer, Integer> buildFrequencyMap(List<Integer> data) {
        Map<Integer, Integer> freq = new HashMap<>();

        for (int value : data) {
            freq.put(value, freq.getOrDefault(value, 0) + 1);
        }

        return freq;
    }

    // Collect values that appear exactly once
    static List<Integer> collectGloballyUniqueValues(List<Integer> data, Map<Integer, Integer> freq) {
        List<Integer> unique = new ArrayList<>();

        for (int value : data) {
            if (freq.get(value) == 1) {
                unique.add(value);
            }
        }

        return unique;
    }

    // Randomly select N values from the unique list
    static List<Integer> selectRandomUnique(List<Integer> unique, int N) {
        if (N > unique.size()) {
            N = unique.size(); // clamp
        }

        // Shuffle the list to randomize order
        List<Integer> temp = new ArrayList<>(unique);
        Collections.shuffle(temp);

        // Take first N elements
        return temp.subList(0, N);
    }

    public static void main(String[] args) {

        // Example list with duplicates
        List<Integer> data = Arrays.asList(
            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
        Map<Integer, Integer> freq = buildFrequencyMap(data);

        // Step 2: Collect values that appear exactly once
        List<Integer> uniqueValues = collectGloballyUniqueValues(data, freq);

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

        // Step 4: Select N random unique values
        List<Integer> randomSelection = selectRandomUnique(uniqueValues, N);

        // Step 5: Print results
        System.out.println("Values that appear exactly once:");
        System.out.println(uniqueValues);

        System.out.println();
        System.out.println("Random selection (" + N + " values):");
        System.out.println(randomSelection);
    }
}


/*
run:

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

Random selection (5 values):
[74, 59, 12, 33, 83]

*/

 



answered Sep 13 by avibootz

Related questions

...