2 Replies - 9100 Views - Last Post: 16 November 2008 - 08:38 PM Rate Topic: -----

#1 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Remove Duplicates in NSMutableArray

Posted 23 October 2008 - 10:57 PM

Is there an easy way to remove duplicate numbers before they are added to an array? I am not a student just an old guy learning Objective-C. Thanks for any help.

Here is my code:

/*__________________________________________________________*/
/*																													*/
/*						  imports													*/
/*__________________________________________________________*/

#import <Foundation/Foundation.h>

/*__________________________________________________________*/
/*																													*/
/*						main listing												*/
/*__________________________________________________________*/

int main (int argc, const char * argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
	
	NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
	[numberFormatter setFormatterbehavior:NSNumberFormatterbehavior10_4];
	[numberFormatter setFormat:@"00"];
	NSString *myNumberString;
	NSMutableArray *array;
	int i;
	NSNumber *newNumber;
	NSNumber *numbersToPrint;

	printf ("\n");
	array = [[NSMutableArray alloc]init];
	// Initialize the random number generator.
	srandom(time(NULL));
	for (i = 0; i < 6; i++) {
		newNumber = [[NSNumber alloc]initWithInt:random() % 55 + 1];
		myNumberString = [numberFormatter stringFromNumber:newNumber];
		[array addObject:myNumberString];
		[array sortUsingSelector:@selector(compare:)];
		[newNumber release];
	}

	// Using NSLog
	for (i = 0; i < 6; i++) {
		numbersToPrint = [array objectAtIndex:i];
		NSLog(@"The number at index %d is %@.", i, numbersToPrint);
	}

	[array release];
	[pool drain];

	return 0;
}

This post has been edited by born2c0de: 10 November 2008 - 02:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Remove Duplicates in NSMutableArray

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Remove Duplicates in NSMutableArray

Posted 09 November 2008 - 05:41 PM

Why don't you use the containsObject selector to determine if you already have that element. Just use the following code inside your for loop.
if (! [array containsObject: myNumberString] )
{
	 //stuff
}


Was This Post Helpful? 0
  • +
  • -

#3 barnwillyb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: Remove Duplicates in NSMutableArray

Posted 16 November 2008 - 08:38 PM

View PostGWatt, on 9 Nov, 2008 - 04:41 PM, said:

Why don't you use the containsObject selector to determine if you already have that element. Just use the following code inside your for loop.
if (! [array containsObject: myNumberString] )
{
	 //stuff
}



Thanks! That worked for me, and that is an easy way indeed!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1