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 9228679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:23:03+00:00 2026-06-18T05:23:03+00:00

I’m trying to create an NSDictionary of unique keys with each value being the

  • 0

I’m trying to create an NSDictionary of unique keys with each value being the count of the unique keys.

i.e.

Given the array [apple, apple, banana, apple, pear, banana] I’d get the dictionary…

{
    apple : 3,
    banana : 2,
    pear : 1
}

I’m sure I’ve seen code before where you can do something like…

NSMutableArray *uniqueKeys = [NSMutableArray array];

for (NSString *string in myArray) {
    uniqueKeys[string]++;
}

But I can’t remember what the exact syntax is.

I know I could do it long hand by extracting the NSNumber and changing it and putting it back but I wanted to use this method if possible.

  • 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-18T05:23:05+00:00Added an answer on June 18, 2026 at 5:23 am

    Seems like you may be able to use NSCountedSet.
    If “apple” and “apple” are equal, you’ll be able to know both how many “fruits” are in your counted-set as well as how many “apple”s are in the set.

    Apple’s Documentation:

    The NSCountedSet class declares the programmatic interface to a
    mutable, unordered collection of indistinct objects. A counted set is
    also known as a bag.

    Each distinct object inserted into an NSCountedSet object has a
    counter associated with it. NSCountedSetkeeps track of the number of
    times objects are inserted and requires that objects be removed the
    same number of times. Thus, there is only one instance of an object in
    an NSSet object even if the object has been added to the set multiple
    times. The count method defined by the superclass NSSet has special
    significance; it returns the number of distinct objects, not the total
    number of times objects are represented in the set. The NSSet and
    NSMutableSet classes are provided for static and dynamic sets
    (respectively) whose elements are distinct.

    Update

    Here is an example:

    NSCountedSet * fruits = [[NSCountedSet alloc] initWithArray:@[@"apple", @"apple", @"banana", @"apple", @"pear", @"banana"]];
    NSLog(@"fruit count = %d", [fruits count]);
    NSLog(@"num apples = %d", [fruits countForObject:@"apple"]);
    NSLog(@"num bananas = %d", [fruits countForObject:@"banana"]);
    NSLog(@"num pears = %d", [fruits countForObject:@"pear"]);
    NSLog(@"num oranges = %d", [fruits countForObject:@"orange"]);
    

    Which will output:

    2013-02-02 13:07:59.058 test[46614:c07] fruit count = 3
    2013-02-02 13:07:59.059 test[46614:c07] num apples = 3
    2013-02-02 13:07:59.062 test[46614:c07] num bananas = 2
    2013-02-02 13:07:59.062 test[46614:c07] num pears = 1
    2013-02-02 13:07:59.063 test[46614:c07] num oranges = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am confused How to use looping for Json response Array in another Array.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.