I have the following problem: i have an array of integers and I would like to put them in a data structure that will have each integer with the numbers of its occurrences, and then sort by the number of occurrences.
So if I have:
[1, 3, 4, 6, 6, 3, 1, 3]
I will have:
[(4,1), (6,2), (1,2), (3,3)]
Where (x,y) == (integer, count of its occurrences).
I tried to use NSCountedSet but it doesn’t work well and I was wondering what’s the best way to do that.
So far I have done the following:
NSCountedSet *totalSet = [[NSCountedSet alloc] initWithArray:finalArray];
Where finalArray is the final array with the full raw data not sorted. totalSet is grouped into (x,y) but not sorted (ideally, should be sorted by ‘y’).
I also tried to do this, but it did not work:
NSArray *sortedArray = [finalArray sortedArrayUsingSelector:@selector(compare:)];
And then do:
NSCountedSet *totalSet = [[NSCountedSet alloc] initWithArray:finalArray];
But that did not change ‘totalSet’.
You could put the numbers and their counts in to an array of dictionaries Like this: