Question

In: Computer Science

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). 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

The above code is to concatenate strings in two array without repetition:

To explain it further:

We have defined two string arrays ; array1 and array2 having strings in both

We have assumed array1 donot have any duplicate values and array2 also, donot have any duplicate values

Also, we can also take input from user for both array in console,

For now , we have declared the strings in the program itself.

Step 1: In the main function:

We have defined array1 and array2 with strings

String[] array1={"Pasta","Macroni","Pizza"};
   String[] array2= {"Rice","Curry","Pizza"};

Also, then function noIdenticalCombine is called having 2 arguments as array1 and array2,

After calling function , return of new array is received, which is array3

String[] array3=noIdenticalCombine(array1,array2);

At last, we printed the values presented in array3 in main function
       for(int i=0;i<array3.length;i++){
System.out.println(array3[i]);
}

Step 2: In the noIdenticalCombine function

noIdenticalCombine(array1, array2)

noIdenticalCombine is a static method having arguments as two string arrays , and return type of string array

In this function , we have compared array2 elements with array1 , if any element is present in array2 which is also there in array1 , flag variable will be incremented ,

flag variable will count the number of repetitive element of array2 with array1

Nested For loop is used to check if the elements of array2 and array1 is same ', if yes increment flag value

for(int i=0;i<array2.length;i++){
element=array2[i];
for(int j=0;j<array1.length;j++){
if(element.equals(array1[j]))
{
flag++; //now flag becomes repitition value of element in array2 with array1
}
}
}

When flag value is updated , new value of array2length is set , by reducing the repetitive value from original array2 length

int array2length = array2.length -flag;

A varible of interger type i is declared so as to iterate from the array3 , to input values in the array

int i;

Also , array3 is decalred with length as array1 + array2 new length
String[] array3= new String[array1.length+array2length];
  

A loop is used to set the values of array3 from array1
for( i=0;i<array1.length;i++){
array3[i]=array1[i];
}

After that , all the elements of array1 is inputted in array3

Here , i value is set now to the next index of array3,

Example: array1 has 5 values , so all values are inputted in array3

Now i has iterated from 0 to 4 , now next index , i is now set to 5

i=5

Now we need to add , non-repetitive array values from array2 , that means , the elements from array2 which are not part of array

So, here also we will use nested loop

and same concept of flag value, used earlier which finding repetitive count

flag=0; //initialize flag with 0
for(int j=0;j<array2.length;j++){
element=array2[j];
for(int k=0;k<array1.length;k++){
if(element.equals(array1[k])) //check for redundant element
{
flag++;
}
  
}
if(flag==0) // if element is unique , add array2 element to next index of array2 ;
{
array3[i]=array2[j];
i++;
}
  
}

At last, the newly updated array is returned from the function and printed in console in main function

return array3;

___________________________________

Please find the below code in JAVA:

public class Main
{
public static String[] noIdenticalCombine(String[] array1, String[] array2){
String element="";
int flag=0;
for(int i=0;i<array2.length;i++){
element=array2[i];
for(int j=0;j<array1.length;j++){
if(element.equals(array1[j]))
{
flag++; //now flag becomes repitition value of element in array2 with array1
}
}
}
int array2length = array2.length -flag;
int i;
String[] array3= new String[array1.length+array2length];
  
for( i=0;i<array1.length;i++){
array3[i]=array1[i];
}

flag=0;
for(int j=0;j<array2.length;j++){
element=array2[j];
for(int k=0;k<array1.length;k++){
if(element.equals(array1[k]))
{
flag++;
}
  
}
if(flag==0)
{
array3[i]=array2[j];
i++;
}
  
}
return array3;
}
   public static void main(String[] args) {
       String[] array1={"Pasta","Macroni","Pizza"};
       String[] array2= {"Rice","Curry","Pizza"};
       String[] array3=noIdenticalCombine(array1,array2);
       for(int i=0;i<array3.length;i++){
System.out.println(array3[i]);
}
   }
}

Please find the output screenshot:


Related Solutions

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...
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...
Could you do it with python please? public class BSTTraversal {         public static void main(String[]...
Could you do it with python please? public class BSTTraversal {         public static void main(String[] args) {                 /**                  * Creating a BST and adding nodes as its children                  */                 BSTTraversal binarySearchTree = new BSTTraversal();                 Node node = new Node(53);                 binarySearchTree.addChild(node, node, 65);                 binarySearchTree.addChild(node, node, 30);                 binarySearchTree.addChild(node, node, 82);                 binarySearchTree.addChild(node, node, 70);                 binarySearchTree.addChild(node, node, 21);                 binarySearchTree.addChild(node, node, 25);                 binarySearchTree.addChild(node, node, 15);                 binarySearchTree.addChild(node, node, 94);                 /**         ...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT