I tested the code, it showed correct output but when verified, it does not pass the createSet() method condition. I suspect it because Math.random() generate some same number in the list of 20 number like: 10 20 23 46 34 10....89. Here I can have 2 number "10", not satify, 20 different numbers
Anyone know a fix for this? Thanks
package com.codegym.task.task08.task0814;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
Greater than 10? You're not a good fit for us
*/
public class Solution {
public static HashSet<Integer> createSet() {
// write your code here
HashSet<Integer> hashSet = new HashSet<>();
for (int i = 0; i < 20; i++) {
Integer num = (int) (Math.random() * 100);
hashSet.add(num);
}
// System.out.println(hashSet);
return hashSet;
}
public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) {
// write your code here
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()) {
if (iterator.next() > 10)
iterator.remove();
}
// System.out.println(set);
return set;
}
public static void main(String[] args) {
HashSet<Integer> hashSet =createSet();
removeAllNumbersGreaterThan10(hashSet);
}
}