7

How do I update the value at index 0

Set<int> set = {1, 2, 3};
set[0] = 0; // error

Note:

I am not looking for workaround like convert Set to List, add element and further convert it back to Set

3 Answers 3

13

Sets elements only have indices incidentally because they are iterables. You can't "update the value at position x" because any update may change the order.

I assume you do want to keep the iteration order (and in that case, you really should be using a list!), so the following won't work:

 void update<T>(Set<T> elements, int index, T newValue) {
   set.remove(set.elementAt(index));
   set.add(newValue);
 }

The problem here is that you won't preserve iteration order, the new value that you added is likely at the end of the iteration (if you are using an insertion-ordered set), or maybe the order has changed completely (if you are not).

An approach which works for insertion-ordered sets:

void replace<T>(Set<T> set, int index, T newValue) {
  if (set.contains(newValue)) throw StateError("New value already in set");
  int counter = 0;
  while (counter < set.length) {
    var element = set.first;
    set.remove(element);
    if (counter == index) element = newValue;
    set.add(element);
    counter++;
  }
}

This repeatedly removes the first element in the set, then inserts it again, except for the indexth element, where it inserts newValue instead. This only works for insertion-ordered sets (like the default LinkedHashSet). (Notice that this will not work if newValue is already in the set, so I made it throw in that case).

For all other sets, there is no better solution that (set.toList()..[index] = newValue).toSet().

7
  • You are so great dude, the understanding you have is just outstanding, thanks a bunch. Would be nice if you can share your secret of Dart!
    – user6274128
    Oct 14, 2019 at 15:18
  • Sir there is one error, set.remove doesn't return you deleted element it returns bool which tells you whether the element you are trying to delete was in set or not. So can you please update your replace method?
    – user6274128
    Oct 15, 2019 at 9:42
  • 1
    Good catch. Updating.
    – lrn
    Oct 17, 2019 at 18:36
  • 1
    What about if we dont have that freedom e.g google maps flutter require polylines and marker in sets rather than list
    – Aimal Khan
    Jul 23, 2020 at 8:23
  • counter does not seem to be updated in the while loop... May 25, 2022 at 17:32
9

You can not change values in a set directly with index, but you can do it with map.

Set<int> set = {1, 2, 3};
Set<int> updated = set.map((item) => item == 1 ? 0 : item).toSet();

print(updated); // => {0, 2, 3}
1
  • Yes, it will work that way but I need to do it using index, not the actual element. I upvoted your post though!
    – user6274128
    Oct 14, 2019 at 13:18
1

You cannot change the value in a set but you could get a similar result by converting it to a list and changing it back to set.

Set<int> set = {1,2,3};
List<int> list = set.toList();
list[1] = 12;
var newset = list.toSet();

this may be a quick solution to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.