.remove()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published May 22, 2024
Contribute to Docs

The .remove() method in Dart is used to remove the first occurrence of a specified element from a queue if it exists.

Syntax

void remove(Object? value)
  • value: The element to be removed from the queue.

Example

In the following example, the element 3 is removed from the queue using the .remove() method:

import 'dart:collection';
void main() {
// Creating a queue
Queue<int> queue = Queue.from([1, 2, 3, 4, 5]);
// Outputting the original queue
print('Original Queue: $queue');
// Removing an element from the queue
queue.remove(3);
// Outputting the modified queue
print('Modified Queue: $queue');
}

The above code results in the following output:

Original Queue: {1, 2, 3, 4, 5}
Modified Queue: {1, 2, 4, 5}

All contributors

Looking to contribute?

Learn Dart on Codecademy