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
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){
Step by Step Solution
3.44 Rating (151 Votes )
There are 3 Steps involved in it
To accomplish this task efficiently in On log n time you can follow these steps Sort both arrays Ini... View full answer
Get step-by-step solutions from verified subject matter experts
