1

How do i go about seeing if my integer is in an array of integers...

eg i want to know if 7 is in an array of [ 1 3 4 5 6 7 8]

any ideas?

Thanks

Sam Jarman
  • 7,277
  • 15
  • 55
  • 100

3 Answers3

6

There are several ways to do this depending on factors such as size of the array - how often you need to search, how often you need to add to the array etc. In general this is a computer science problem.

More specifically I'd guess there are three options likely to best fit your needs.

  1. "Brute force": just loop through the array looking for the value. Calling containsObject: on the NSArray will do this for you. Simple and probably fastest for small array sizes.
  2. Copy the array into a set and use containsObject: to check for existence
  3. Keep the values in the array, but sort the array and implement your own binary search - which is probably not as complex as it sounds.
philsquared
  • 22,403
  • 12
  • 69
  • 98
  • No.1 did the trick.. the array had about 16 items. Cheers Phil – Sam Jarman Apr 12 '10 at 08:35
  • Oh wow, looking at this question now, I just feel silly. In 2011 I started my computer science degree, and now I have a good understanding of searching algorithms. – Sam Jarman Jun 28 '12 at 05:24
  • Excellent - how would you do it these days? ;-) – philsquared Jul 07 '12 at 17:44
  • Depending on the assumptions I can make about the data, anything. If its sorted, then a divide and conquer approach would be good, otherwise might have to stick with a linear search. – Sam Jarman Jul 13 '12 at 21:26
3

This depends on the type of array you have, if it's an object or a C array. Judging by your tags you've got an NSArray with NSIntegers, this would be wrong. NSIntegers are not objects and cannot be put into an NSArray, unless you wrap them into an object, for example an NSNumber.

NSArray

Use the containsObject: method.

I'm not entirely sure how you put your integers into an NSArray. The usual way to do this is to use NSNumber.

NSArray *theArray = [NSArray arrayWithObjects:[NSNumber numberWithInteger:1],
                                              [NSNumber numberWithInteger:7],
                                              [NSNumber numberWithInteger:3],
                                              nil];
NSNumber *theNumber = [NSNumber numberWithInteger:12];
/*
 * if you've got the plain NSInteger you can wrap it
 * into an object like this:
 * NSInteger theInt = 12;
 * NSNumber *theNumber = [NSNumber numberWithInteger:theInt];
 */
if ([theArray containsObject:theNumber]) {
    // do something
}

C-Array

I suspect you're using a C-Array. In that case you have to write your own loop.

NSInteger theArray[3] = {1,7,3}
NSInteger theNumber = 12;
for (int i; i < 3; i++) {
    if (theArray[i] == theNumber) {
        // do something
        break; // don't do it twice
               // if the number is twice in it
    }
}
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • Judging by the tags he used it looks like he's using NSIntegers in an NSArray – philsquared Apr 12 '10 at 08:20
  • @Phil Nash: That's what I thought too, but using plain NSIntegers in an NSArray isn't possible. NSInteger isn't an object, it's just a typecasted int or long. – Georg Schölly Apr 12 '10 at 08:23
  • you are right of course :-) I really hate having to do boxing and unboxing in Objective-C. Good job we can use Monotouch instead. Oh wait... – philsquared Apr 12 '10 at 08:49
0
//assume these data, either from a method call or instance variables
int theArray[7] = {1,7,3,8,5,7,4};
int numberIWant = 8;

//this is the essence in a C-array, which you can easily use on ios
BOOL isNumberFound = NO;
for (int i; i < sizeof(theArray)/sizeof(int); i++) {
    if (theArray[i] == numberIWant) {
        isNumberFound = YES;
        break; //breaks the for loop               
    }
}
//return the bool, or otherwise check the bool

if (isNumberFound)
{
//do stuff
}
Nicki
  • 984
  • 8
  • 7