Set vs List when need both unique elements and access by index

Sunny :

I need to keep a unique list of elements seen and I also need to pick random one from them from time to time. There are two simple ways for me to do this.

  1. Keep elements seen in a Set - that gives me uniqueness of elements. When there is a need to pick random one, do the following:

    elementsSeen.toArray()[random.nextInt(elementsSeen.size())]
    
  2. Keep elements seen in a List - this way no need to convert to array as there is the get() function for when I need to ask for a random one. But here I would need to do this when adding.

    if (elementsSeen.indexOf(element)==-1) {elementsSeen.add(element);}
    

So my question is which way would be more efficient? Is converting to array more consuming or is indexOf worse? What if attempting to add an element is done 10 or 100 or 1000 times more often?

I am interested in how to combine functionality of a list (access by index) with that of a set (unique adding) in the most performance effective way.

binoternary :

If using more memory is not a problem then you can get the best of both by using both list and set inside a wrapper:

public class MyContainer<T> {
    private final Set<T> set = new HashSet<>();
    private final List<T> list = new ArrayList<>();

    public void add(T e) {
        if (set.add(e)) {
            list.add(e);
        }
    }

    public T getRandomElement() {
        return list.get(ThreadLocalRandom.current().nextInt(list.size()));
    }
    // other methods as needed ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

container with both unique elements and access by index

Array of index values for unique elements in list

.index() show index of both similar elements from a list

Is it possible to set both a unique index and value at the same time in a javascript array

Is there a function to create a list of unique elements in unique index using python?

series.unique vs list of set - performance

BsonDocument contains an array. Need to access the array elements in C# when the BsonDocument is contained by a C# list

Access multiple elements of list knowing their index

I need to print Null if there are no unique elements in an Array list

Checking for unique elements in a string vs. converting string to a set

Count number of unique elements at index in 2D list

Add a range of elements to a list at a given index, replacing the its contents from that index in the list and resizing it (if need it)

MongoDB Update with upsert and unique index atomicity when document is not in the working set

How do I access elements in a list by index in DAML?

Filter List for unique elements

Filter List for unique elements

Unique list elements in Tcl

Stop processing when the number of consecutive list elements have a unique value

Indexing a list with an unique index

How to get a set of elements in a list based on element index in Scala?

Time complexity for adding elements to list vs set in python

Argument Error in Elixir when trying to access a list by index with list[i]

Retrieve Index Number of Unique Elements

Postgres unique constraint vs index

mongoDB mongoose unique index vs unique

UnboundLocalError when trying to delete multiple list elements by index

List Index Out Of Bounds when deleting elements from TObjectList

MySql unique index vs. index speed

How to access individual elements of a list when using pipe operator?