I have an exrecise on which i have to write a method which get Queue and remove each part of it who`s in the queue more than once for example: for 11223344 It wil return a queue with 1234
How can i do it?
I have an exrecise on which i have to write a method which get Queue and remove each part of it who`s in the queue more than once for example: for 11223344 It wil return a queue with 1234
How can i do it?
You can use the Distinct()
function of an IEnumerable
:
Queue<int> a = new Queue<int>(); // This is the queue from which you want to remove duplicates.
Queue<int> result = new Queue<int>(a.Distinct());
A Queue is essentially a collection, thus an IEnumerable and hence the LINQ extension methods for IEnumerable types can be called.
Here's a demo : .NET Fiddle Link
This is basically a duplication:
Depending on you input:
More reads of how distinct works: How does LINQ .distinct method sort?