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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:38:53+00:00 2026-05-16T01:38:53+00:00

How would I compare two arrays that might have different lengths and get the

  • 0

How would I compare two arrays that might have different lengths and get the difference between each array?

For example:

Cat cat = new Cat();
Dog dog = new Dog();
Alligator alligator = new Alligator();

Animal animals[] = { cat, dog };
Animal animals2[] = { cat, dog, alligator };

How would I compare them two arrays and make it return the instance of Alligator?

  • 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-16T01:38:53+00:00Added an answer on May 16, 2026 at 1:38 am

    I would suggest that your question needs to be clarified. Currently, everyone is guessing what about what you are actually asking.

    • Are the arrays intended to represent sets, or lists, or something in between? In other words, does element order matter, and can there be duplicates?
    • What does “equal” mean? Does new Cat() “equal” new Cat()? Your example suggests that it does!!
    • What do you mean by the “difference”? Do you mean set difference?
    • What do you want to happen if the two arrays have the same length?
    • Is this a once-off comparison or does it occur repeatedly for the same arrays?
    • How many elements are there in the arrays (on average)?
    • Why are you using arrays at all?

    Making the assumption that these arrays are intended to be true sets, then you probably should be using HashSet instead of arrays, and using collection operations like addAll and retainAll to calculate the set difference.

    On the other hand, if the arrays are meant to represent lists, it is not at all clear what “difference” means.

    If it is critical that the code runs fast, then you most certainly need to rethink your data structures. If you always start with arrays, you are not going to be able to calculate the “differences” fast … at least in the general case.

    Finally, if you are going to use anything that depends on the equals(Object) method (and that includes any of the Java collection types, you really need to have a clear understanding of what “equals” is supposed to mean in your application. Are all Cat instances equal? Are they all different? Are some Cat instances equal and others not? If you don’t figure this out, and implement the equals and hashCode methods accordingly you will get confusing results.

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

Sidebar

Related Questions

I have two arrays that I would like to compare and ultimately wind up
I would like to compare two dates in javascript. I have been doing some
I have been trying to compare two arrays. Using array_intersect presents no problems. When
I would like to compare two tables and then update if some logic is
What would be a better approach to compare two floating point values - using
I would like to compare a screenshot of one application (could be a Web
I would like to compare the contents of a couple of collections in my
What would be the benefit of using decimal.compare vs. just using a > or
How can I compare strings in a case insensitive way in Python? I would
I have an NSMutableArray that is populated with objects of strings. For simplicity sake

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.