Produce a method that identifies the common elements between two arrays. The method adds the similar elements
Question:
Produce a method that identifies the common elements between two arrays. The method adds the similar elements to an ArrayList and returns it. Must be done in O(n log n) time. This method can only use an auxiliary ArrayList (no other collections should be used). Call this method: similar
Parameter(s): String[], String[]
Returns: ArrayList
Ex. Input: {"Volvo", "BMW", "Ford", "Mazda"} and {"Mazda", "Test", "BMW", "Volvo"}, Output: {BMW, Mazda, Volvo}
This is the code I have so far, I need help with the rest of the code:
import java.util.*; class Main { public static void main(String[] args) { //Code for testing String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; String[] cars2 = {"Mazda", "Test", "BMW", "Volvo"}; System.out.println(similar(cars, cars2)); } public static ArrayList similar (String [] cars, String [] cars2){
Algorithm Design And Applications
ISBN: 9781118335918
1st Edition
Authors: Michael T. Goodrich, Roberto Tamassia