Skip to content

Instantly share code, notes, and snippets.

@xhruso00
Created February 22, 2015 05:34
Show Gist options
  • Save xhruso00/5fe72ec211293d4bff7b to your computer and use it in GitHub Desktop.
Save xhruso00/5fe72ec211293d4bff7b to your computer and use it in GitHub Desktop.
Code snippet to find the number of pairs given by the difference in given array of unique numbers
int numberOfPairs(NSSet *uniqueNumbers, int difference) {
if (difference <= 0 || [uniqueNumbers count] <= 0) {
return 0;
}
//creating set of decremented numbers to do intersection
NSMutableSet *setToCompare = [[NSMutableSet alloc] init];
for (NSNumber *number in uniqueNumbers) {
[setToCompare addObject:@([number intValue] - difference)];
}
//intersection will cause that only pairs will stay within given set
[setToCompare intersectSet:uniqueNumbers];
return (int)[[setToCompare allObjects] count];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment