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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:45:54+00:00 2026-06-12T09:45:54+00:00

Is there any easy way to check if two arrays contain any common elements?

  • 0

Is there any easy way to check if two arrays contain any common elements?
Is this appropriate? The arrays contain type char.

Arrays.asList(encryptU).contains(Ualpha[randNum]));

thanks in advance!

  • 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-12T09:45:56+00:00Added an answer on June 12, 2026 at 9:45 am

    If the arrays are small, then a solution with a nested for loop (e.g. @scaryrawr’s) is going to perform best.

    If the arrays are large enough, then the O(N^2) complexity of the above solution will be problematic. The solution is to use a HashSet; e.g.

    HashSet<Character> tmp = new HashSet<Character>();
    for (char ch : arr1) {
        tmp.add(ch);
    }
    for (char ch : arr2) {
        if (tmp.contains(ch)) {
            // elements in common!!
        }
    }
    

    This is O(N) in time, though the constant of proportionality is rather large. (I think you’d need the product of the array sizes to be 20 or 30 for this to be faster than the nested loop solution … but that is a guess.) Also, this requires O(N) temporary space.


    If the range of the characters is limited, then you could use a BitSet instead of a HashSet. That will also be roughly O(N) in time and space, though the range of the characters is also a factor in the complexity, so calling it O(N) is an over-simplification.


    But we are probably “over-thinking” this. The best advice would probably be to implement something simple, and if there is a suspicion that performance is a real concern then profile it to avoid wasting your time with unnecessary optimizing.

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

Sidebar

Related Questions

I just want to check if there is any easy way to limit zooming.
Is there any easy way to calculate the number of lines changed between two
I wonder if there is an easy way to check if two strings match
Is there any easy way to check if one array contains another array in
Is there any easy way to check whether a path is valid? The file
Is there any easy way to automatically deploy a web service / java web
Is there any easy way to tell perl now ignore everything that is printed?
Is there any EASY way to sort an array in descending order like how
Is there any easy way to change the color of the UITableViewCellAccessoryCheckmark from standard
Is there any easy way to do so?

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.