Home > Software engineering >  Find all non-matching items, based on an input parameter, in an array and return it as a new array
Find all non-matching items, based on an input parameter, in an array and return it as a new array

Time:09-29

I have an array of company names and I want to see if they match a certain value.

In fact, I want a list of all non-matching values. Something like this:

"One or more companies: [company1], [company2] are not equal to [checkCompany]

Example:

checkCompany = "GM"
companies = ["GM", "GM", "Ford"]

In this case there would only be one company not equal to "GM" but there could be more than one.

CodePudding user response:

This function should simply work, its arguments are the companies array and the checkCompanies string that you want to check against.

The function then returns tempList which includes all the values that do not match with checkCompanies string.

    import java.util.ArrayList;
    public ArrayList<String> checkList(ArrayList<String> companies, String checkCompany) {
    ArrayList<String> tempList = new ArrayList<String>();

    for(int i = 0; i < companies.size(); i  ) {
        if(!companies.get(i).equals(checkCompany)) {
            tempList.add(companies.get(i));
        }
    }

    return tempList;

All you would then have to do is format the return to give you the string you want to output!

CodePudding user response:


    checkCompany = "GM"
    companies = ["GM", "GM", "Ford"]
    nonMatchedCompanies = list()
    for(String company : companies){
        if(!company.contains(checkCompany)
            nonMatchedCompanies.add(company)
    }

nonMatchedCompanies is the result which will contain all not matched companies from the list. This will check if the checkCompany is part of company name.

  • Related