//4种方法
对数组中的元素去重复
NSArray
*array =
@[
@"12-11"
,
@"12-11"
,
@"12-11"
,
@"12-12"
,
@"12-13"
,
@"12-14"
]
;
array = [array
valueForKeyPath
:
@"@distinctUnionOfObjects.self"
];
NSLog(@"%@", array);
//字典去重
NSMutableDictionary
*resultDict = [[
NSMutableDictionary
alloc
]
initWithCapacity
:array.
count
];
for
(
NSString
*item
in
array) {
[resultDict
setObject
:item
forKey
:item];
}
NSArray
*resultArray = resultDict.
allValues
;
NSLog(@"%@", resultArray);
//
如果需要按照原来的升序排序,可以这样
resultArray
=
[
resultArray
sortedArrayUsingComparator
:
^
NSComparisonResult
(
id
_Nonnull
obj1
,
id
_Nonnull
obj2
)
{
NSString
*item1
=
obj1
;
NSString
*item2
=
obj2
;
return
[
item1
compare
:item2
options
:NSLiteralSearch
]
;
}
]
;
NSLog
(
@"%@"
,
resultArray
)
;
//有序集合
NSOrderedSet
*set = [
NSOrderedSet
orderedSetWithArray
:array];
NSLog
(
@"%@"
, set.
array
);