In: Computer Science
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.
}
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: