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)....
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
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 //...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
How would you write the following recursively? This is in Java.    public static String allTrim(String...
How would you write the following recursively? This is in Java.    public static String allTrim(String str) { int j = 0; int count = 0; // Number of extra spaces int lspaces = 0;// Number of left spaces char ch[] = str.toCharArray(); int len = str.length(); StringBuffer bchar = new StringBuffer(); if (ch[0] == ' ') { while (ch[j] == ' ') { lspaces++; j++; } } for (int i = lspaces; i < len; i++) { if (ch[i]...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
Create a class called FileSumWrapper with a method that has the signature public static void handle(String...
Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound). Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT