0

Without knowing the range and when the array is unordered, so there is no need to sort the array ascendingly and get the first element

So I have this method:

- (NSInteger) lowestNumberInArray:(NSArray *)arrayOfNumbers {

Parameter: array of NSNumbers

Return: The lowest number in the array as an NSInteger

I am thinking of a for loop that loops through the array, once the lowest number is found then store that number into a NSInteger. However, I don't know the range of the array values aka I don't know what's my biggest and lowest number. I looked into NSArray and NSMutableArray documentation and didn't find any method that I can use to return the smallest value. Your help will be greatly appreciated! :)

- (NSInteger) lowestNumberInArray:(NSArray *)arrayOfNumbers {

    smallest = equalBiggestNumber;
    for (NSInteger i = 0; i < arrayOfNumbers.count; i++) {
        if (arrayOfNumbers[i] < smallest) {
            smallest = arrayOfNumbers[i];
        }

    return 0;
}
ioskaveen
  • 196
  • 1
  • 4
  • 14
  • according to your code in your question, the smallest number is always 0 ;P – Fonix Sep 02 '15 at 03:48
  • can your array have negative numbers in them? – cream-corn Sep 02 '15 at 03:48
  • @cream-corn yes they can have negative numbers. – ioskaveen Sep 02 '15 at 03:58
  • this code, if it is what you are actually using, will not compile. `smallest` is not declared as any type. `NSNumber *smallest` or `NSInteger smallest`, you are missing a closing brace to your loop. and just FYI you `return 0` all the time. so no matter what your loop actually works out. it will return 0 – cream-corn Sep 02 '15 at 04:26
  • @KerolosNakhla Consider accepting answers that are helpful. To accept an answer click on the hollow checkmark next to the answer that is best, doing so will increase your reputation and allow more capabilities, See [reputation faq](http://stackoverflow.com/faq#reputation) See [this page](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) for more detail. Also please consider going back and accepting past answers, doing so will increase your reputation and allow more capabilities, See [reputation faq](http://stackoverflow.com/faq#reputation) – zaph Sep 02 '15 at 13:38

4 Answers4

3

You can use KVC and the collection operators for this:

NSNumber* smallest = [arrayOfNumbers valueForKeyPath:@"@min.self"];

If the array elements were objects and you wanted the smallest property value, you'd use that property's key instead of "self" in the key path. As it happens, self is a "property" of all objects and NSNumbers are directly comparable, so you can use "self" there.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
2

You don't need to. The initial value of smallest can be any number of numberArray. You can just:

smallest = arrayOfNumbers[0];

OR if you really want to:

smallest = NSIntegerMax;

Then you do the for loop. if you assign smallest to the first object in the array you can skip it in the loop.

NSInteger smallest = arrayOfNumbers[0];
for (NSInteger i = 1; i < arrayOfNumbers.count; i++) {

    if (arrayOfNumbers[i] < smallest) {
        smallest = arrayOfNumbers[i];
    }
}

return smallest;

}

cream-corn
  • 1,820
  • 14
  • 26
Franz Wang
  • 596
  • 4
  • 14
  • @Rajatp Edited. You misunderstand it. – Franz Wang Sep 02 '15 at 03:43
  • To clarify (for Rajatp and others), SugaR is suggesting that you do `smallest = arrayOfNumbers[0]` before the loop. The loop is still needed to find if any other element of the array is smaller, although it can be adjusted to start at index 1 since index 0 has already been examined. – Ken Thomases Sep 02 '15 at 03:55
  • @KenThomases Exactly. Thank you. Sorry for being unclear. – Franz Wang Sep 02 '15 at 04:05
  • @SugaR so I made smallest = arrayOfNumbers[0]; but then got an error message that says smallest (inside the loop) is an undeclared identifier. What are your thoughts? – ioskaveen Sep 02 '15 at 04:14
1

You can try this out

int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue];

or

NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"];
with numbers as an NSArray

Version 1: sort the array:

NSArray *sorted1 = [numbers sortedArrayUsingSelector:@selector(compare:)];
// 1.585 seconds

Version 2: Key-value coding, using "doubleValue":

NSNumber *max=[numbers valueForKeyPath:@"@max.doubleValue"];
NSNumber *min=[numbers valueForKeyPath:@"@min.doubleValue"];
// 0.778 seconds

Version 3: Key-value coding, using "self":

NSNumber *max=[numbers valueForKeyPath:@"@max.self"];
NSNumber *min=[numbers valueForKeyPath:@"@min.self"];
// 0.390 seconds

Version 4: Explicit loop:

float xmax = -MAXFLOAT;
float xmin = MAXFLOAT;
for (NSNumber *num in numbers) {
    float x = num.floatValue;
    if (x < xmin) xmin = x;
    if (x > xmax) xmax = x;
}
// 0.019 seconds

Version 5: Block enumeration:

__block float xmax = -MAXFLOAT;
__block float xmin = MAXFLOAT;
[numbers enumerateObjectsUsingBlock:^(NSNumber *num, NSUInteger idx, BOOL *stop) {
    float x = num.floatValue;
    if (x < xmin) xmin = x;
    if (x > xmax) xmax = x;
}];
// 0.024 seconds

I get this answer from this link Finding the smallest and biggest value in NSArray of NSNumbers

Finding maximum numeric value in NSArray

Community
  • 1
  • 1
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
0

Your question is a little confusing. How could you not know the size of the array? Does the array keep on getting values added in?

-- If the array is consistently getting values added in at random times, you should create a notification. Then whenever there is a new number added in, compare the newly added number to see if that is your new lowest number.

hope that helps!!!

jjlei
  • 109
  • 7