开发者

Remove Elements from one Queue that are present in another queue

开发者 https://www.devze.com 2023-01-26 23:32 出处:网络
Using C# I have Queue N: 3, 4 9, 11 Queue A: 1, 2, 3, 4, 8, 9, 11, 12, 13 I want to remove all elements from Queue A that are present in Queue N

Using C#

I have

Queue N: 3, 4 9, 11

Queue A: 1, 2, 3, 4, 8, 9, 11, 12, 13

I want to remove all elements from Queue A that are present in Queue N

And end up with Queue R: 1, 2, 8, 12, 13

How do I do this in C#

Trying to work with someones API and they offer a way to get the two qu开发者_运维问答eues and I need to filter based on one queue returned.

Thanks,

-Seth

Updated Code Example:

I am using a custom data type ModuleDetails

Queue<ModuleDetails> defaultQueue = apiCallDefault();
Queue<ModuleDetails> modQueue = apiCallAllModules();


Easiest to do it with LINQ-to-Objects...

var qN = new Queue<int>(new[]{3, 4, 9, 11});
var qA = new Queue<int>(new[]{1, 2, 3, 4, 8, 9, 11, 12, 13});
var qR = qA.Except(qN);

UPDATE: As long as ModuleDetails implements IEquatable, the code below will work just fine. If you can't modify ModuleDetails, you can always supply an IEqualityComparer as the second argument of Except, which defines how equality is determined.


It's important to note that if your queue was filled with your custom objects, then you would have to implement IEqualityComparer interface. Obviously you won't need it for the int type since it already implements the interface.


If you can use linq, look at Except method.

int[] first  = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] second = new int[] {             4, 5, 6,         10 };

var q = first.Except(second);
0

精彩评论

暂无评论...
验证码 换一张
取 消