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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:23:21+00:00 2026-05-18T02:23:21+00:00

I have two arrays of objects which are likely to have the same values,

  • 0

I have two arrays of objects which are likely to have the same values, but in a different order, e.g.

{ "cat", "dog", "mouse", "pangolin" }

{ "dog", "pangolin", "cat", "mouse" }

I wish to treat these two arrays as equal. What’s the fastest way to test 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-18T02:23:21+00:00Added an answer on May 18, 2026 at 2:23 am

    I can’t guarantee that this is the fastest, but it’s certainly quite efficient:

    bool areEquivalent = array1.Length == array2.Length 
                         && new HashSet<string>(array1).SetEquals(array2);
    

    EDIT:
    SaeedAlg and Sandris raise valid points about different frequencies of duplicates causing problems with this approach. I can see two workarounds if this is important (haven’t given much thought to their respective efficiencies):

    1.Sort the arrays and then compare them sequentially. This approach, in theory, should have quadratic complexity in the worst case.
    E.g.:

    return array1.Length == array2.Length
           && array1.OrderBy(s => s).SequenceEqual(array2.OrderBy(s => s));
    

    2.Build up a frequency-table of strings in each array and then compare them. E.g.:

    if(array1.Length != array2.Length)
       return false;
    
    var f1 = array1.GroupBy(s => s)
                   .Select(group => new {group.Key, Count = group.Count() });
    
    var f2 = array2.GroupBy(s => s)
                   .Select(group => new {group.Key, Count = group.Count() });
    
    return !f1.Except(f2).Any();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays which contain objects of assets, now I want to subtract
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays of custom objects. Both have properties of NSString *name. I
I have two arrays of different size A and B. I am comparing A
I have two arrays. The first array contains the sort order. The second array
I have two arrays. One is $categories which contains all categories retrieved from my
I have an array which contains objects some may be same and some are
I have two arrays of objects that describe the structure of a neural network,
i have two mutable arrays that i am looping through when the objects are
I have two arrays of objects: var a = [ {'id': 20}, {'id': 15},

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.