Greater than 10? You're not a good fit for us

  • 10
  • Locked
The Oddiozo transshipment center is looking for employees. The only requirement is this: employees must not have more than ten limbs (we don't know where this form of discrimination came from). We'll help them with a program that will create a set of numbers (Set ), add 20 different numbers, and then remove from the set all the numbers greater than 10.
You can't complete this task, because you're not signed in.
Comments (17)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
ImDevin
Level 15 , Old Town, United States
29 April, 02:36
wow, this was a super learning experience task. Found three different ways to solve this task. practiced writing it to gain familiarity. Nearly took all day, but it was worth it. Happy coding! :)
Desislava Petrova
Level 24 , Sofia, Bulgaria
17 February, 10:10
set.removeIf(n -> (n > 10));
Nickolas Johnson
Level 8 , St. Louis, United States of America
20 November 2020, 16:04
set.removeAll came in super handy here
Karas Java Developer
16 September 2020, 00:21
Enhanced for loop in the second one, had two create an additional Set but it worked.
Liew
Level 10
11 May 2020, 07:48
why is it when u call iterator, to remove the element you call it. remove instead of set.remove(it) as according to hash set java API???
Adrian
Level 26 , Spanish Town, Jamaica
30 April 2020, 06:17
I got this right using the removeIf method in the Set class. we haven't been taught that here, but I stumbled across it while reading some time ago and was itching to try it. for what we have been taught though, just: 1. create an iterator object to go through the set (a set doesn't have index numbers so there is no .get() method. 2. run the interation as long as there is another number in the set (.hasNext()) 3. if statement that removes numbers larger than 10, called by the iterator. cheers! :)
Andrei
Level 41
3 November 2020, 09:54
Have you also tried using iterator.remove(); ? I tried doing your way but I did not understand (from my research) how to use removeIf() function so I did it with remove.
Liliane Top
Level 17 , Amsterdam, Netherlands
16 February 2020, 18:45
I'm very confused about the usage of HashSet vs Set. In the given examples I encounter Set<String> set = new HashSet<String>(); but in the task above it gives an error with the return statement. When I changed this into: HashSet<String> set = new HashSet<String>(); it worked. I'm so confused. What is the difference? And why give examples if they do not work? Can anybody shed some light on this matter? Very much appreciated!
Andrei
Level 41
3 November 2020, 09:17
Hello! I have stumbled upon the same thing. Have you found any explanation for this? Thanks! :)
Turner
Level 11 , Pittsburgh, United States
30 August 2019, 05:38
Can I assume that a solution for removeAllNumbersGreaterThan10() - first, iterate through the set and set all numbers > 10 to a separate array then iterate through the array removing the set elements WITH NO CONCURRENCY issues fails the requirement is because of Time complexity? Not sure if Iteration<Integer> introduces a savings of Space complexity, but an extra for loop does NOT increase performance much beyond O(n). Can you explain what I am missing in why this solution is not allowed to pass? Please advise.
mani
Level 13 , Harpanahalli, India
28 June 2019, 12:41
can you please help to find my errors
Dino
Level 13 , Zagreb, Croatia
10 January 2019, 20:58
Can anyone tell me what doesn't work here? Thanks! package com.codegym.task.task08.task0814; import java.util.HashSet; import java.util.Set; import java.util.Iterator; /* Greater than 10? You're not a good fit for us */ public class Solution { public static HashSet<Integer> createSet() { // write your code here int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; HashSet<Integer> set = new HashSet<Integer>(); for(int aa: a) set.add(aa); return set; } public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) { // write your code here for(Iterator<Integer> iterator = set.iterator(); iterator.hasNext();) { Integer aa = iterator.next(); if (aa > 10) set.remove(aa); } return set; } public static void main(String[] args) { } }
Guadalupe Gagnon
Level 37 , Tampa, United States
10 January 2019, 21:13
hit the help button at the top and ask your question. Do not copy/paste your code...... use the slider:
Ed Maphis
Level 20 , Painesville, United States
23 May 2019, 19:26
Hmmm, I didn't know that.