Given an input NSArray, I am trying to return all unique permutations that have a length of n. The ordering is not important. My code works when n is 0 or 1. However, if n is bigger than 1 it also returns all the computed permutations that have a length that is lower than n. I only want the permutations that are of length n.

Here is my code:

```
+ (NSMutableSet *) combinations: (NSArray *) inputArray n: (int) n {
NSMutableSet *permutations = [[NSMutableSet alloc] init];
[ permutations addObject: [[NSSet alloc] init] ];

for (int i=0; i<n; i++) {
    NSMutableSet *newPermutations = [[NSMutableSet alloc] init];
    for (int j=0; j<[inputArray count]; j++) {
        NSSet *single = [NSSet setWithObjects:inputArray[j],nil];
        for (id permutation in permutations) {
            NSSet *newPermutation = [permutation setByAddingObjectsFromSet:single];
            [newPermutations addObject: newPermutation];
        }
    }
    permutations = [newPermutations copy];
}

return permutations;
}

```

Can anyone spot what is wrong here? I start with an empty set, then bread-first wise expand it. By assigning the newPermutations variable to permutations after each loop it should remove all the permutations from the previous level.
This is my first day using Objective-C so I don't know why it is not behaving like I think it should.