Why this program doesn't pass tests? It works fine for me.

public static HashSet<Integer> createSet()
{
    Set<Integer> set = new HashSet<Integer>();
    for (int i = 1; i < 20; ++i)
        set.add(i);
    return (HashSet<Integer>)set;
    //add your code here

}

public static HashSet<Integer> removeAllNumbersMoreThan10(HashSet<Integer> set)
{
    HashSet<Integer> toRemove = new HashSet<Integer>();
    for (int number : set)
        if (number > 10)
            toRemove.add(number);
    for (int number : toRemove)
        set.remove(number);
    return set;
    //add your code here

}

asked 15 Apr, 12:45

noelo's gravatar image

noelo
102
accept rate: 0%


When number > 10 you need to remove element. Passed test.

public class Solution
{
    public static HashSet<Integer> createSet()
    {
        HashSet<Integer> set = new HashSet<Integer>();
        for(int i=0; i < 20; i++){
            set.add(i);
        }
        return set;
    }

    public static HashSet<Integer> removeAllNumbersMoreThan10(HashSet<Integer> set)
    {
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            if(it.next() > 10){
                it.remove();
            }
        }
        return set;
    }
}
link

answered 26 May, 11:36

Igor's gravatar image

Igor
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×16

Asked: 15 Apr, 12:45

Seen: 167 times

Last updated: 26 May, 11:36