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

  • Home
  • SEARCH
  • 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 6915049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:25:33+00:00 2026-05-27T09:25:33+00:00

I have an NSArray containing custom objects, for example: [A, A, B, C, A,

  • 0

I have an NSArray containing custom objects, for example:

[A, A, B, C, A, D, E, B, D]

What is the best way to group these items so the end result resembles this?

A: 3
B: 2
C: 1
D: 2
E: 1

Please note that the duplicates are all different instances that have the same properties, but I have overridden isEqual: for this.

  • 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-05-27T09:25:34+00:00Added an answer on May 27, 2026 at 9:25 am

    The simplest way is probably to use an NSCountedSet. You can use [NSCountedSet setWithArray:myArray] to produce a counted set of your array, and then you can iterate over the set’s contents to find out the count of each object in the set. Note that it won’t be sorted.

    Also note that you’ll need to provide a sensible implementation of -hash for this to work, since you only said you overrode -isEqual:. You also need to override -compare: if you need a sorted list of results.

    Here’s a quick method that takes your array and prints the count of each element, sorted:

    void printCountOfElementsInArray(NSArray *ary) {
        NSCountedSet *set = [NSCountedSet setWithArray:ary];
        NSArray *objs = [[set allObjects] sortedArrayUsingSelector:@selector(compare:)];
        for (id obj in objs) {
            NSLog(@"%@: %d", obj, [set countForObject:obj]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have an NSArray with 40 objects. What is the most efficient way
I have an NSArray formed with objects of a custom class. The class has
I have a NSArray containing objects. I want to create a secondary NSArray containing
I have an NSArray containing numbers as NSString objects. ie. [array addObject:[NSString stringWithFormat:@%d, 100]];
I have an NSMutableDictionary, analyzedPxDictionary , containing a bunch of Pixel objects (a custom
Another puzzler. I have an NSArray (iTours) containing 3 Objects and a retain count
I have an NSArray and I'd like to create a new NSArray with objects
I have an NSArray of (Product) objects that are created by parsing an XML
I have an NSArray of NSDictionary objects which I would like to be able
If I have an NSArray with some values in them. Is there a way

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.