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:...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
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...
write a java program that allows main to work public static void main(String[] args) { /*...
write a java program that allows main to work public static void main(String[] args) { /* Start with the empty list. */ LinkedList list = newLinkedList(); // // ******INSERTION****** // // Insert the values deleteFront(list); deleteBack(list); list = insert(list, 1); list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list = insert(list, 7); list = insert(list, 8);    // Basic Operations on the LinkedList printList(list); insertFront(list,0); printList(list); insertBack(list,999); printList(list);...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT