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