JAVA Arrays 1. Write a method called explode that, for each element in the input
ID: 3877418 • Letter: J
Question
JAVA
Arrays
1. Write a method called explode that, for each element in the input array, the frequency of each element is
doubled in a new primitive array that is subsequently returned. That is, an input array consisting of 2 3 4 5
results in a new array containing 2 2 3 3 4 4 5 5.
2. Write a method called implode that removes redundant elements from a given sorted array (and inserts
zeros at the end of the array). That is, an input array consisting of 2 2 3 3 3 4 5 5 will result in 2 3 4
5 0 0 0 0.
3. Write a method called reverse that reverses the content of a given array (first goes to last, second goes to
second-to-last etc.).
4. Write a method called unique that returns true or false if the input array contains only unique values (no
duplicates). Do not assume the input array is sorted.
5. Write a method called merge that combines the contents of two sorted input arrays into one sorted array (that
is subsequently returned from the method). That is, merging 2 4 7 10 and 1 3 7 11 results in an array
containing 1 2 3 4 7 7 10 11. Your implementation must sort as you go: it is not allowed to add both
arrays to a single array and then sort that array.
6. Write a method called consolidate that removes all non-positive integers from the given array so that the
remaining integer values are in adjacent positions (and zeros fill all space at the end). That is, an input array
consisting of 0 2 -1 0 5 7 0 3 10 results in a new array containing 2 5 7 3 10 0 0 0 0.
7. Write a method called search that finds the occurrence of a target in a given array.
SAMPLE CODE MUST MATCH
Test 'explode' method
aryIn has:
1 2 3 4 5 6
aryOut has:
1 1 2 2 3 3 4 4 5 5 6 6
Test 'implode' method
Before processing aryOut has:
1 1 2 2 3 3 4 4 5 5 6 6
After processing aryOut has:
1 2 3 4 5 6 0 0 0 0 0 0
Test 'reverse' method
Before processing aryIn has:
1 2 3 4 5 6
After processing aryOut has:
6 5 4 3 2 1
Test 'unique' method
aryIn has:
6 5 4 3 2 1
Is it Unique? true
aryOut has:
1 2 3 4 5 6 0 0 0 0 0 0
Is it Unique? false
Test 'merge' method
aryLeft has:
2 4 7 10
aryRight has:
1 3 7 11
After merge:
1 2 3 4 7 7 10 11
Test 'consolidate' method
aryIn6 before 'consolidate' has:
0 2 -1 0 5 7 0 3 10
aryIn6 after 'consolidate' has:
2 5 7 3 10 0 0 0 0
Test 'search' method
target '7' in aryIn6? (index or -1 if not found) : 2
target '20' in aryIn6? (index or -1 if not found) : -1
SKELETON CODE
public class Arrays {
public static String printArray(int[] ary) {
String result = "";
//To be completed
return result;
}
public int[] explode(int[] ary) {
int[] ex = new int[ary.length * 2];
//To be completed
return ex;
}
public void implode(int[] ary) {
//To be completed
}
public void reverse(int[] ary) {
//To be completed
}
public boolean unique(int[] ary) {
//To be completed
return true;
}
public int[] merge(int[] left, int[] right) {
int[] merged = new int[left.length + right.length];
//To be completed
return merged;
}
public void consolidate(int[] ary) {
//To be completed
}
int search(int[] array, int target) {
int index = 0;
boolean found = false;
//To be completed
return found ? index : -1;
}
}
Explanation / Answer
Code:
public class Arrays {
//converts the array element as the string
public static String printArray(int[] ary) {
String result = "";
for (int i=0; i<ary.length; i++)
result = result + ary[i]+ " ";
return result;
}
//explode function to duplicate the elements
public int[] explode(int[] ary) {
int[] ex = new int[ary.length * 2];
int count =0;
for (int i =0; i<ary.length; i++)
{
ex[count++]= ary[i];
ex[count++] = ary[i];
}
return ex;
}
//implode function to remove the duplicate elements
public void implode(int[] ary) {
int[] temp = new int[ary.length];
int j = 0;
for (int i=0; i<ary.length-1; i++){
if (ary[i] != ary[i+1]){
temp[j++] = ary[i];
}
}
temp[j++] = ary[ary.length-1];
for (int i=j; i<ary.length; i++){
temp[i] = 0;
}
// Changing original array
for (int i=0; i<ary.length; i++){
ary[i] = temp[i];
}
}
public void reverse(int[] ary) {
int j = ary.length - 1; // last element
int i = 0; // first element
int temp;
//interchanges the element in the array
while(i<j)
{
temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
i++;
j--;
}
}
//checks for unique element in the array
public boolean unique(int[] ary) {
for (int i=0; i<ary.length; i++)
{
// Check if the picked element is present in the array
int j;
for (j=0; j<i; j++)
if (ary[i] == ary[j])
return false;
}
return true;
}
//merges two array
public int[] merge(int[] left, int[] right) {
int[] merged = new int[left.length + right.length];
int k = 0;
for (int i=0; i<left.length; i++)
merged[k++] = left[i];
for (int i=0; i<right.length; i++)
merged[k++] = right[i];
return merged;
}
//removes -1 and 0 from the array
public void consolidate(int[] ary) {
int[] temp = new int[ary.length];
int j = 0;
for (int i=0; i<ary.length-1; i++){
if ((ary[i] != -1 )&& (ary[i] != 0 )){
temp[j++] = ary[i];
}
}
temp[j++] = ary[ary.length-1];
// assigning remaing with zeroes
for (int i=j; i<ary.length; i++){
temp[j++] = 0;
}
for (int i=0; i<ary.length; i++){
ary[i]=temp[i];
}
}
//search the element in the array
int search(int[] array, int target) {
int index = 0;
boolean found = false;
for (index = 0; index<array.length; index++){
if (array[index] == target ){
found = true;
break;
}
}
return found ? index : -1;
}
public static void main (String args[]){
int array1[] = {1,2,3};
int[] array2 = new int[array1.length * 2];
Arrays array = new Arrays();
array2 = array.explode(array1);
System.out.print(" Test Explode method "+ "aryln has : "+printArray(array1));
System.out.print(" arryOut has :"+ printArray(array2));
System.out.print(" Test Implode method "+ "Before processing aryOut has : "+printArray(array2));
array.implode(array2);
System.out.print(" arryOut has :"+ printArray(array2));
System.out.print(" Test Reverse method "+ "Before processing aryOut has : "+printArray(array1));
array.reverse(array1);
System.out.print(" arryOut has :"+ printArray(array1));
System.out.print(" Test unique method "+ "Before processing aryOut has : "+printArray(array1));
System.out.print(" Is it unique ?"+ array.unique(array1));
int leftarray[] = {1,2,3};
int rightarray[] = {4,5,6,7};
System.out.print(" Test merge method "+ "aryLeft has : "+printArray(leftarray));
System.out.print(" aryRight has :"+ printArray(rightarray));
int[] mergearray = new int[leftarray.length +rightarray.length];
mergearray = array.merge(leftarray, rightarray);
System.out.print(" After Merge :"+ printArray(mergearray));
int array3[] = {0, 2, -1, 0, 5, 7, 0, 3, 10};
System.out.print(" Test consolidate method "+ "ary before consolidate has : "+printArray(array3));
array.consolidate(array3);
System.out.print(" ary after consolidated has :"+ printArray(array3));
System.out.print(" Test search method "+ "ary has : "+printArray(array1));
System.out.print(" Target 1 in ary ?(index or -1 if not found) :"+ array.search(array1, 1));
}
}
OUTPUT:
run:
Test Explode method
aryln has : 1 2 3
arryOut has :1 1 2 2 3 3
Test Implode method
Before processing aryOut has : 1 1 2 2 3 3
arryOut has :1 2 3 0 0 0
Test Reverse method
Before processing aryOut has : 1 2 3
arryOut has :3 2 1
Test unique method
Before processing aryOut has : 3 2 1
Is it unique ?true
Test merge method
aryLeft has : 1 2 3
aryRight has :4 5 6 7
After Merge :1 2 3 4 5 6 7
Test consolidate method
ary before consolidate has : 0 2 -1 0 5 7 0 3 10
ary after consolidated has :2 5 7 3 10 0 0 0 0
Test search method
ary has : 3 2 1
Target 1 in ary ?(index or -1 if not found) :2BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.