0

Is there an easy way to transform an array of numbers to an arrays with the numbers in sequence?

NSArray *numbers = @[@1,@2,@5,@3];

// Transformed arrays
//NSArray *numbersInSequence = @[@1,@2,@3];
//NSArray *numbersInSequence2 = @[@5];

EDIT:

I modified the code in Richard's answer to get it to work.

NSArray *arraysBySplittingNumbersInOrder(NSArray *input) {

// sort 'input'
input = [input sortedArrayUsingSelector:@selector(compare:)];

NSMutableArray *results = [NSMutableArray array];

if (input.count) {

    int start = 0;
    int last = INT_MIN;

    for (int i = 0; i < input.count; i++) {

        BOOL lastItem = i == input.count - 1;

        // The first item of the array
        if (i == 0) {

            if (lastItem) {
                [results addObject:input];
                break;
            }

            last = [input[i] intValue];
            continue;
        }

        int cur = [input[i] intValue];

        if (cur != last + 1) {

            // pull out the next array
            [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];

            start = i;
        }

        // The last item of the array
        if (lastItem) {

            [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start + 1)]];            
        }

        last = cur;
    }
}

return results;
}
Community
  • 1
  • 1
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • You can find the answer here: http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – Dave Nov 27 '12 at 12:49
  • @Dave no, that's not necessary. He just needs to sort it using the built in `-compare:` selector, and then traverse the array once. – Richard J. Ross III Nov 27 '12 at 12:57

2 Answers2

0

I don't think there's an easy way to do this; you'll probably have to do at least part of the work yourself.

My suggestion would be to sort the array an then iterate through it, building the sections as you go. Whenever you hit a "jump", i.e. a non-consecutive number, this concludes your current section and starts a new one.

waldrumpus
  • 2,540
  • 18
  • 44
0

Here's a rather simple solution:

NSArray *arraysBySplittingNumbersInOrder(NSArray *input)
{
    // sort 'input'
    input = [input sortedArrayUsingSelector:@selector(compare:)];

    NSMutableArray *results = [NSMutableArray array];

    if (input.count)
    {
        int start = 0;
        int last = INT_MIN;

        for (int i = 0; i <= input.count; i++)
        {
            if (i == 0)
            {
                last = [input[i] intValue];
                continue;
            }
            if (i == input.count)
            {
                if (i != start + 1)
                {
                    [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];
                    continue;
                }
            }

            int cur = [input[i] intValue];

            if (cur != last + 1)
            {
                // pull out the next array
                [results addObject:[input subarrayWithRange:NSMakeRange(start, i - start)]];
                start = i;
            }

            last = cur;
        }
    }

    return results;
}

int main()
{
    NSArray *input = @[ @1, @3, @4, @7, @8, @12, @13, @14 ];

    NSLog(@"%@", input);
    NSLog(@"%@", arraysBySplittingNumbersInOrder(input));
}

Output:

2012-11-27 07:55:04.609 TestProj[35890:303] (
    1,
    3,
    4,
    7,
    8,
    12,
    13,
    14
)
2012-11-27 07:55:04.611 TestProj[35890:303] (
        (
        1
    ),
        (
        3,
        4
    ),
        (
        7,
        8
    ),
        (
        12,
        13,
        14
    )
)
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201