remove method

bool remove(
  1. E element
)

Removes the first occurrence of the specified element from this bag, if it is present. If the Bag does not contain the element, it is unchanged. Does this by overwriting with the last element and then removing the last element. Returns true if this list contained the specified element.

Implementation

bool remove(E element) {
  for (var i = 0; i < size; i++) {
    final current = _data[i];

    if (element == current) {
      // overwrite item to remove with last element
      _data[i] = _data[--_size];
      // null last element, so gc can do its work
      _data[size] = null;
      return true;
    }
  }

  return false;
}