Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:25:31+00:00 2026-06-15T04:25:31+00:00

I read on some SO thread on making an unique array, unfortunately, it did

  • 0

I read on some SO thread on making an unique array, unfortunately, it did not work for me.

BackGround Info –

  • I have an objectArray filled with 20 object in it.
  • a NSObject sampleData with a field NSNumber * number;
  • maxCount is a int which user input how many random numbers they want.
  • below is my code in Xcode and the return for calling the function

.m

-(IBAction) testButton
{
[self chooseNumber];
}

-(void)chooseNumber
{
    maxCount = [numberOfClues.text intValue];
   // NSInteger rdmNumber = arc4random()%objectArray.count;

    int count = 0;
    do {

        NSInteger rdmNumber = arc4random()%objectArray.count;
        if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]])
        {
            currentData.number = [NSNumber numberWithInt:rdmNumber];
            [dataArray addObject:currentData];
            currentData=nil;
            currentData= [[sampleData alloc]init];
            count++;    
            NSLog(@"random no - %d",rdmNumber);
        }
    } while (count < maxCount);
    NSLog(@"Array got %d numbers",dataArray.count);
}

return value from NSLog

2012-11-29 08:26:50.888 randomNumbers[1255:11303] random no - 5
2012-11-29 08:26:50.892 randomNumbers[1255:11303] random no - 8
2012-11-29 08:26:50.926 randomNumbers[1255:11303] random no - 5
2012-11-29 08:26:50.930 randomNumbers[1255:11303] random no - 10
2012-11-29 08:26:50.933 randomNumbers[1255:11303] random no - 4
2012-11-29 08:26:50.946 randomNumbers[1255:11303] random no - 10
2012-11-29 08:26:50.949 randomNumbers[1255:11303] random no - 9
2012-11-29 08:26:50.952 randomNumbers[1255:11303] random no - 12
2012-11-29 08:26:50.955 randomNumbers[1255:11303] random no - 0
2012-11-29 08:26:50.957 randomNumbers[1255:11303] random no - 15
2012-11-29 08:26:50.960 randomNumbers[1255:11303] random no - 1
2012-11-29 08:26:50.963 randomNumbers[1255:11303] random no - 8
2012-11-29 08:26:50.964 randomNumbers[1255:11303] random no - 18
2012-11-29 08:26:50.966 randomNumbers[1255:11303] random no - 14
2012-11-29 08:26:50.968 randomNumbers[1255:11303] random no - 12
2012-11-29 08:26:50.977 randomNumbers[1255:11303] random no - 14
2012-11-29 08:26:50.980 randomNumbers[1255:11303] random no - 3
2012-11-29 08:26:50.983 randomNumbers[1255:11303] random no - 6
2012-11-29 08:26:50.986 randomNumbers[1255:11303] random no - 15
2012-11-29 08:26:50.989 randomNumbers[1255:11303] random no - 7
2012-11-29 08:26:50.992 randomNumbers[1255:11303] Array got 20 numbers

How do I make it such that the array will get all unique number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:25:32+00:00Added an answer on June 15, 2026 at 4:25 am

    The problem lies here

    if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]]) {
         currentData.number = [NSNumber numberWithInt:rdmNumber];
         [dataArray addObject:currentData];
    

    You are checking for NSNumber instances but you don’t put NSNumber inside the array, you are putting something completely different (currentData).

    If you need both ordering (do you really need ordering?) and uniqueness, a combination of NSArray and NSSet can be used for effectiveness. Of course, this is more effective only if you do lots of operations which needs the uniqueness check. If you only want to create an array and and use it without changes, your implementation (using [NSArray containsObject:] will be better.
    Abstracting everything into a special class is always a good solution:

    @interface UniqueArray
    
    @property (nonatomic, strong, readonly) NSArray* elements;
    
    - (BOOL)addObject:(id)object;
    
    @end
    
    @interface UniqueArray ()
    
    @property (nonatomic, strong, readwrite) NSArray* elements;
    @property (nonatomic, strong, readwrite) NSMutableSet* set;
    
    @end
    
    @implementation UniqueArray
    
    - (id)init {
       self = [super init];
    
       if (!self) {
          return nil;
       }
    
       self.elements = [NSMutableArray array];
       self.set = [NSMutableSet set];
    
       return self;
    }
    
    - (BOOL)addObject:(id)object {
       if ([self.set containsObject:object]) {
          return NO;
       }
    
       [self.set addObject:object];
       [(NSMutableArray*) self.elements addObject:object];
    
       return YES;
    }
    
    @end
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some thread to write resource and some to read it.But pthread_rwlock cause
I have read some threads regarding this and I did already take steps to
I have read many questions considering thread-safe double checked locking (for singletons or lazy
CLOCK_MONOTONIC does not seem available, so clock_gettime is out. I've read in some places
I'm posting this thread because I have some difficulties to deal with pictures in
I have a web service and some methods and I do NOT want to
I read some XSLT examples and found that code: <xsl:apply-template select=@*|node()/> What does that
I read some tutorial in Google App Engine, in those tutorial there is same
I read some articles about Comet tech. All of them mentioned that the long-life
I read some of the discussion in this question and thought to myself that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.