We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 57863bb commit 3ea012bCopy full SHA for 3ea012b
src/main/java/com/thealgorithms/randomized/ReservoirSampling.java
@@ -26,6 +26,10 @@ public class ReservoirSampling {
26
* @return A list containing k randomly selected elements.
27
*/
28
public static List<Integer> sample(int[] stream, int sampleSize) {
29
+ if (sampleSize > stream.length) {
30
+ throw new IllegalArgumentException("Sample size cannot exceed stream size.");
31
+ }
32
+
33
List<Integer> reservoir = new ArrayList<>(sampleSize);
34
Random rand = new Random();
35
0 commit comments