Skip to content

Instantly share code, notes, and snippets.

@coderRohan123
Created January 16, 2024 05:15
Show Gist options
  • Save coderRohan123/585d0e03ee2f1db0e7698af46adfb0e7 to your computer and use it in GitHub Desktop.
Save coderRohan123/585d0e03ee2f1db0e7698af46adfb0e7 to your computer and use it in GitHub Desktop.
The code snippet is a class that implements a randomized set. It allows for searching, inserting, and removing elements. The getRandom() method returns a random element from the set.

Randomized Set Implementation

Preview:
class RandomizedSet {
    private ArrayList<Integer> list;
    private Map<Integer, Integer> map;

    public RandomizedSet() {
        list = new ArrayList<>();
        map = new HashMap<>();
    }

    public boolean search(int val) {
        return map.containsKey(val);
    }

    public boolean insert(int val) {
        if (search(val)) return false;

        list.add(val);
        map.put(val, list.size() - 1);
        return true;
    }

    public boolean remove(int val) {
        if (!search(val)) return false;

        int index = map.get(val);
        list.set(index, list.get(list.size() - 1));
        map.put(list.get(index), index);
        list.remove(list.size() - 1);
        map.remove(val);

        return true;
    }

    public int getRandom() {
        Random rand = new Random();
        return list.get(rand.nextInt(list.size()));
    }
}
Associated Context
Type Code Snippet ( .java )
Associated Tags RandomizedSet class ArrayList data structure HashMap data structure search method insert method remove method getRandom method Random number generation Searching for a given value ArrayList Map HashMap Data structure
πŸ“ Custom Description PLEASE UPVOTE IF IT HELPED

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
πŸ’‘ Smart Description This code snippet defines a class called RandomizedSet that represents a set of integers. It has methods to search, insert, remove, and random integer values from the list using Map's HashMap method.
The code snippet is a class that implements a randomized set. It allows for searching, inserting, and removing elements. The getRandom() method returns a random element from the set.
πŸ”Ž Suggested Searches Java code for creating a randomized set with search and remove methods
Related Links https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/4572728/Beats-99.84-Users-oror-C%2B%2BJavaPythonJavaScript-oror-EXPLAINED
https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/
https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/?currentPage=1&orderBy=hot&query=
https://www.geeksforgeeks.org/arrays-in-java/
https://www.geeksforgeeks.org/arraylist-in-java/
https://www.geeksforgeeks.org/java/
https://mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
https://www.codejava.net/java-core/collections/java-list-collection-tutorial-and-examples
https://www.programiz.com/java-programming/hashmap
https://www.geeksforgeeks.org/design-a-data-structure-that-supports-insert-delete-search-and-getrandom-in-constant-time/
https://www.baeldung.com/java-random-list-element
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://rohan016.pieces.cloud/?p=8d354389e8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment