如何删除nsarray中的重复项。例如,我的数组包含以下数据。我想与相邻的日期进行比较以避免重复,但它通过错误。
任何人都可以指导我出错的地方
日历第一 -
(
2010-09-25 17:00:00 GMT,
"AAA",
2010-09-25 17:00:00 GMT,
"AAA",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-26 17:00:00 GMT,
"BBB",
2010-09-27 17:00:00 GMT,
"CCCC",
2010-09-27 17:00:00 GMT,
"CCC",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-28 17:00:00 GMT,
"AAA",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-29 17:00:00 GMT,
"DDDD",
2010-09-30 17:00:00 GMT,
"BBBB"
)
我的代码
NSArray dates; //dates contain above values
NSMutableArray *temp_date = [[NSMutableArray alloc] init];
for (int i=0; i<[dates count]; i+=2){
BOOL day;
if ([dates count]-2 >i) {
day = [[dates objectAtIndex:i] compare:[dates objectAtIndex:i+2]];
}
if (day) {
[temp_date addObject:[dates objectAtIndex:i]];
[temp_date addObject:[dates objectAtIndex:i+1] ];
}
}
此致 Sathish所在
答案 0 :(得分:46)
你可以使用套装。创建后将自动删除重复项。
NSArray *cleanedArray = [[NSSet setWithArray:dates] allObjects];
请记住,集合是无序的。
根据布伦特的评论,这个问题的最新解决方案将允许您按顺序保留元素。
[[NSOrderedSet orderedSetWithArray:dates] array]
答案 1 :(得分:7)
最干净的方式
<html>
<head>
<title>Springy.js image node demo</title>
</head>
<body>
<script src="jquery-1.11.3.js"></script>
<script src="springy.js"></script>
<script src="springyui.js"></script>
<script src="bluebird.min.js"></script>
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>-->
<script/>
$.ajax({
url: 'https://rawgit.com/theresajbecker/CompBio/master/SuperSmallNodes.json',
type: 'GET',
dataType: 'json'
}).done(function(graphP) {
for (var i = 0; i < graphP.elements.nodes.length; i++) {
var node = graphP.elements.nodes[i].data.id;
var Nmass = graphP.elements.nodes[i].data.mass;
console.log('mass');
console.log(graphP.elements.nodes[i].data.mass);
console.log('node id');
console.log(graphP.elements.nodes[i].data.id);
console.log('station_name');
console.log(graphP.elements.nodes[i].data.station_name);
}
for (i = 0; i < graphP.elements.edges.length; i++) {
var Nnode = graphP.elements.edges[i].data.source;
console.log('edges source');
console.log(graphP.elements.edges[i].data.source);
console.log('edges target');
console.log(graphP.elements.edges[i].data.target);
//console.log(node)
}
var graph = new Springy.Graph();
for ( i = 0; i < graphP.elements.nodes.length; i++)
{
var NGnode = graph.newNode({ label: 'graphP.elements.nodes[i].data.id'});
}
for ( i = 0; i < graphP.elements.edges.length; i++) {
var edge = graph.newEdge({source: graphP.elements.edges[i].data.target},
{target: graphP.elements.edges[i].data.target});
}
$(document).ready(function(){
var springy = window.springy = jQuery('#springydemo').springy({
graph: graph,
nodeSelected: function(node){
console.log('Node selected: ' + JSON.stringify(node.data));
},
repulsion: 500
});
});
});
</script>
<div>
<canvas id="springydemo" width="800" height="400" style="border: 1px solid black;"></canvas>
</div>
</body>
</html>
答案 2 :(得分:1)
要查找相同的对象,最好使用–indexOfObjectIdenticalTo:
或 - indexOfObjectIdenticalTo:inRange:
答案 3 :(得分:1)
如果您使用过mutableArray,请使用此选项。
NSArray *copy = [mutableArray copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator]) {
if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
[mutableArray removeObjectAtIndex:index];
}
index--;
}
[copy release];