logo Practice-It logo

removeInRange

Language/Type: Java hashing implementing a collection
Related Links:
Author: Marty Stepp (on 2013/02/19)

Consider the following class HashSet, a set of elements implemented using a hash table with separate chaining. Assume that the class is implemented in the following way:

public class HashSet implements Set {
    private Node[] elements;
    private int size;
    ...

    private class Node {
        private E data;
        private Node next;
        ...
    }
}

Add a method to this class named removeInRange that accepts a minimum and maximum value and removes from the set any elements that are between the given minimum and maximum, inclusive. For example, if a hash set in a variable set stores [31, 12, 22, 45, 6, 28, 18, 59], after calling set.removeInRange(10, 30);, the set should store [31, 45, 6, 59]. You should assume that the element type E is comparable and cast it to type Comparable so that you can determine which elements are in the given range. You should not create any arrays or temporary data structures. This method should run in O(N) time, where N is the number of total elements stored in the set.

(NOTE: To be compatible with Practice-It and avoid conflicting with Java's java.util.HashSet, our HashSet is renamed HashSet2 here.)

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.