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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:44:51+00:00 2026-06-12T17:44:51+00:00

I have searched this on the internet, but have found no solution… Here is

  • 0

I have searched this on the internet, but have found no solution…

Here is what I want:

Say I have an NSArray with three objects. These three objects are:

{

@"Hi",
@"Hi",
@"Hi"

 }

In this case the array contains three strings, which are all equal.

I want to testify for this, I want to create an NSArray category BOOLmethod that loops through all the objects and returns YES if they are all identical. Here is an example:

NSArray *array = [[NSArray alloc] initWithObjects: @"Object", @"Object", @"Object", nil];

if ([array allObjectsAreIdentical /* method I would have to create */) {

     NSLog(@"All objects in this array are identical");
}

I am pretty sure that there is no built in method for this, and also I would prefer not to take the tedious route and manually compare each object.

Thanks!

  • 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-12T17:44:53+00:00Added an answer on June 12, 2026 at 5:44 pm

    What about

    NSSet *set = [NSSet setWithArray:array];
    if ([set count] == 0) {
         // array is empty. Does that count as identical ?
    } else if ([set count] == 1) {
         // all objects in array are identical
    } else {
         // there are different objects
    }
    

    EDIT: Written as NSArray category method this would be

    -(BOOL) allObjectsAreIdentical
    {
        NSSet *set = [NSSet setWithArray:self];
        return ([set count] <= 1);
    }
    

    Note: You should be aware that this solution (even if it might look elegant) compares more objects in the array than necessary. Therefore, if performance is an issue, you better go with other solutions, e.g. the one proposed by @dieworld.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched for a general solution to this but only find answers to
I have searched internet and stackoverflow thoroughly, but I haven't found answer to my
I have searched on internet about implementation of Segment trees but found nothing when
I don't understand. I have searched all internet forums but found nothing helpful. I
Preface: I am sure this is incredibly simple, but I have searched this site
I have searched for this, and i found that for api less than 12,
I have searched and searched for this but I think my terminology isn't correct
I have searched for this but nobody have the same issue.. Now I need
So I have searched for this but not finding anything about it and if
I have searched but can't find why this simple code will fail in Drools

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.