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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:50:06+00:00 2026-05-28T14:50:06+00:00

Given 2 arrays of String values such as: String[] a1 = {A, B, C}

  • 0

Given 2 arrays of String values such as:

String[] a1 = {"A", "B", "C"}  
String[] a2 = {"A", "B"};  

where one array(a1) contains all the available String values and another array(a2) contains value I do not want to consider, how can I return an array with elements from a1 not contained in a2?

Constraints:
1> a1 and a2 will always contain unique valid values i.e. any value in a2 will always be a value from a1 and so there can never be a mismatch between a1 and a2)
2> a2 can be empty array

Here is what I have in mind:

List<String> nonMatch = new ArrayList<String>(a1.length - a2.length);
for (String a : a2)
{
    if (Arrays.binarySearch(a1, a) < 0)
    {
            nonMatch.add(a);
     }
}
return nonMatch.toArray();

But I wanted to know if there are any better solutions without degrading with performance

  • 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-28T14:50:07+00:00Added an answer on May 28, 2026 at 2:50 pm

    I would use a Set<T> – probably a HashSet<T>. For example:

    Set<String> results = new HashSet<String>(Arrays.asList(a1));
    results.removeAll(Arrays.asList(a2));
    return results; // Convert to an array if you really must
    

    EDIT: My previous edit seems to have got lost, annoyingly.

    I personally wouldn’t convert back to an array unless you’ve got a compelling reason to do so. Life is generally more pleasant in Java if you stick to the Java Collection APIs (List, Set, Map etc) rather than arrays. You should also look at Guava which contains a bunch of nice functionality. In this particular case it wouldn’t do much to improve things:

    Set<String> results = Sets.newHashSet(a1);
    results.removeAll(Arrays.asList(a2));
    return results;
    

    … but in general it’s an incredibly useful library to have up your sleeve.

    EDIT: To preserve order, use LinkedHashSet<T>:

    Set<String> results = new LinkedHashSet<String>(Arrays.asList(a1));
    results.removeAll(Arrays.asList(a2));
    return results; // Convert to an array if you really must
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string. I need to replace all instances of a given array
I have string that I want to chop to array of substrings of given
Given a string with attribute/value pairs such as attr1=some text attr2 = some other
Given the array below, how do I get values of object(stdClass) such as Pcv
i want to run a loop on a string type array, such that if
How do I create an array in smarty from a given string like 22||33||50
Given two sorted arrays: A and B . The size of array A is
Duplicate In C arrays why is this true? a[5] == 5[a] Given an array
All I need is to compare dates represented by string values in the format
Given an array of n Objects, let's say it is an array of strings

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.