Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:52:51+00:00 2026-06-15T19:52:51+00:00

I have the following problem: i have an array of integers and I would

  • 0

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’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:52:52+00:00Added an answer on June 15, 2026 at 7:52 pm

    You could put the numbers and their counts in to an array of dictionaries Like this:

        NSArray *arr = @[@1, @3, @4, @6, @6, @3, @1, @3];
        NSCountedSet *totalSet = [NSCountedSet setWithArray:arr];
        NSMutableArray *dictArray = [NSMutableArray array];
        for (NSNumber *num in totalSet) {
            NSDictionary *dict = @{@"number":num, @"count":@([totalSet countForObject:num])};
            [dictArray addObject:dict];
        }
        NSArray *final = [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES ]]];
        NSLog(@"%@",final);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all, I have an array of long that I would like to write
This is the problem: I have the following array (from $wpdb->get_results() ): array(6) {
I have a library which expects a array and fills it. I would like
i have the following problem: Create a program where a particle will execute a
I am having the following problem that I cannot solve. Let's say I have
I have the following code which return wmi information (unknown array) For Each objMgmt
I have the following array structure: Array ( [t] => 812 [0] => Array
I came across the following Dynamic Programming problem. You have a grid of integers
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.