Created
February 22, 2015 05:34
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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