从两个数组中获取唯一数字 [英] Getting unique numbers from two arrays

查看:61
本文介绍了从两个数组中获取唯一数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有几个 NSArrays 填充了 intsNSNumbers,就像这样:

If I have a couple of NSArrays filled with ints, or NSNumbers, like so:

A: { 12, 23, 45, 56, 67, 78, 99, 234 }
B: { 12, 56, 78, 99, 454, 512 }

如何输出一个数组,其中的数字在 A 中但不在 B 中,例如

How do I output an array with numbers that are in A but not in B, like

{ 23, 45, 67, 234 }

推荐答案

您要做的只是一个集合操作.所以你可以在这里使用 NSSet.你应该做 minusSet: 以获得你想要的结果.

What you are tying to do is purely a set operation. So you can use NSSet here. You should do minusSet: to get the result you want.

NSMutableSet *resultSet = [NSMutableSet setWithArray:A];
NSSet *setB = [NSSet setWithArray:B];

// This is what you need!
[resultSet minusSet:setB];

Array *result = [resultSet allObjects];

这篇关于从两个数组中获取唯一数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆