Find additional string array string

Given two arrays of strings, A and B.
B contains every element in A, and has one additional member, for example:
* A = [‘dog’, ‘cat’, ‘monkey]
* B = [‘cat’, ‘rat’, ‘dog’, ‘monkey’]
Write a function to find the extra string in B. Do this in O(n)

Solution: Please have below java program to find additional string array string :

package com.javahonk;

public class TwoArraysOfStringsCompare {

    public static void main(String[] args) {

        String[] a = { "dog", "cat", "monkey" };
        String[] b = { "cat", "rat", "dog", "monkey" };
        StringBuilder stringBuilder = new StringBuilder();

        for (String string : a) {
            stringBuilder.append(string + " ");
        }

        for (String string : b) {

            if (!stringBuilder.toString().contains(string)) {
                System.out.println("Extra animal contains in b: "
                                + string);
            }

        }

    }

}

Output:

Find additional string array string

Leave a Reply

Your email address will not be published. Required fields are marked *