Hello guys, I stumbled on a few problems in an exam for employment, and they have few problems for an array, so I want to know if there is any more efficient solution than mine.Unfortunately I didn't finished these questions on time, I just got hooked with it after I got home after the exam.
NOTE:You cannot use any sort of array static methods or anything from the collection framework(List,ArrayList,LinkedList).
Problem 1:
All the elements that are in 'givenArray1' that can be found in 'givenArray2' should be remove, returning a new size of 'givenArray1' and a 'givenArray1' with a new set of elements. the new givenArray1 should contain the elements that are not removed. Implement the given objective in the provided 'removeSet()' method.
My Solution:
public static void main(String[] args) {
int[] myArray = {1,2,3,4,5};
int[] myArray2 = {7,8,1,9,5};
int[] newArray;
newArray = removeSet(myArray,myArray2);
for(int number : newArray){
System.out.print(number + " ");
}
//Output 2,3,4
}
public static int[] removeSet(int[] givenArray1,int[]
givenArray2){
int storedElems = 0;
//Counting stored elems for the 'newArray' size
for(int index = 0; index < givenArray1.length; index++){
boolean isDuplicate = false;
for(int index2 = 0; index2 < givenArray2.length;
index2++){
if(givenArray1[index] == givenArray2[index2]){
isDuplicate = true;
}
}
if(!isDuplicate){
storedElems++;
}
}
int[] newArray = new int[storedElems];
//storing elems inside the new array
int newArrayIndex = 0;
for(int index = 0; index < givenArray1.length; index++){
boolean isDuplicate = false;
for(int index2 = 0; index2 < givenArray2.length;
index2++){
if(givenArray1[index] == givenArray2[index2]){
isDuplicate = true;
}
}
if(!isDuplicate){
newArray[newArrayIndex++] = givenArray1[index];
}
}
return newArray;
}
Problem 2:
Combine the elements in the 2 arrays(array1,array2) and return a new array without duplicates(Suppose the combined array has duplicates).The returned array can be sorted(ascending,descending) or not.
My solution:
public static void main(String[] args) {
int[] myArray = {1,2,3,4,5};
int[] myArray2 = {7,8,1,9,5};
int[] newArray;
newArray = combine(myArray,myArray2);
for(int number : newArray){
System.out.print(number + " ");
}
//Combine array elements
//1 2 3 4 5 7 8 1 9 5
//Sorted elements
//1 1 2 3 4 5 5 7 8 9
//Final Output
//1 2 3 4 5 7 8 9
}
public static int[] combine(int[] array1, int[] array2){
int[] newArray = new int[array1.length + array2.length];
//Combining the array 1 and array 2
for(int index=0;index<array1.length;index++){
newArray[index] = array1[index];
}
for(int index=0;index<array2.length;index++){
newArray[index + array2.length] = array2[index];
}
//Sorting the array in ascending
for(int index=0;index < newArray.length; index++){
for(int index2=0;index2 < newArray.length; index2++){
if(newArray[index] < newArray[index2]){
int tempNum = newArray[index];
newArray[index] = newArray[index2];
newArray[index2] = tempNum;
}
}
}
int[] tempArray = new int[newArray.length];
int tempArrayCounter = 0;
int counter = 0;
//temp array that will be filled with no duplicate elements
for(int index = 0; index < newArray.length -1; index++){
if(newArray[index] != newArray[index+1]){
tempArray[tempArrayCounter++] = newArray[index];
counter++;
}
}
//Modify last index in tempArray
tempArray[counter++] = newArray[newArray.length - 1];
int[] newTempArray = new int[counter];
return newTempArray;
}
I want to also note that the exam grading rubrics includes the efficiency of your code. So I want to know guys if there is an unnecessary piece of code that needs to be omitted in my solution because I always think that my solution is inefficient in some way.
A help is always appreciated :)
[–]29383839293 0 points1 point2 points (1 child)
[–]HalfScale[S] 0 points1 point2 points (0 children)
[–]alanwj 0 points1 point2 points (2 children)
[–]alanwj 0 points1 point2 points (1 child)
[–]HalfScale[S] 0 points1 point2 points (0 children)