Question

In: Computer Science

Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...

Write in Java:

Write a method called:

public static String[] noIdenticalCombine(String[] array1, String[] array2)

{

// instructions: returns an array that contains all the Strings in array1 and array2 but without repetition.

order does not matter, but it will return array1's elements and then array2's element that are not in array1.

Assume there are no duplicates are in array1 and array2.

Could use count which is how many str there are in array2, where !contains(array1, str). May an array of length array1.length + count. Copy elements from array1 into new array then copy elements in array2 where !contains(array1, str). Return the new array.

Both array1 and array2 are not null or empty. OR strings in array1 or array2 is null.

}

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

import java.util.Arrays;

class Main{
    //Combine two strings and elimiate duplicates
    public static String[] noIdenticalCombine(String[] array1, String[] array2)
    {
        //Find the common elements in array2 which are in array1
        int count = 0;
        for(int i=0;i<array2.length;i++){
            boolean found = false;
            for(int j=0;j<array1.length;j++){
                if(array2[i].equals(array1[j])){
                    found = true;
                    break;
                }
            }
            if(!found){
                count++;
            }
        }
        //Create a new array
        String[] ans = new String[array1.length+count];
        //Copy first array to the answer
        for(int i=0;i<array1.length;i++){
            ans[i] = array1[i];
        }
        int ind = array1.length;
        for(int i=0;i<array2.length;i++){
            boolean found = false;
            for(int j=0;j<array1.length;j++){
                if(array2[i].equals(array1[j])){
                    found = true;
                    break;
                }
            }
            //If element is not present in array1, add it to ans
            if(!found){
                ans[ind++] = array2[i];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        String a[] = {"a","b","c","d"};
        String b[] = {"b","d","e","f"};
        System.out.println(Arrays.toString(noIdenticalCombine(a,b)));
    }
}

Related Solutions

How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT