This is for Codewars Kata, with Java.
the brief is : Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b keeping their order.
If a value is present in b, all of its occurrences must be removed from the other: Kata.arrayDiff(new int[] {1, 2, 2, 2, 3}, new int[] {2}) => new int[] {1, 3}
the Problem: I already have solved the brief, but when return it, instead of the value i got the memory address of the arrays.
import java.util.*;
public class Kata {
public static int[] arrayDiff(int[] a, int[] b) {
ArrayList<Integer> listA = new ArrayList<Integer>();
ArrayList<Integer> listB = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
listA.add(a[i]);
// listA.get(i);
}
for (int j = 0; j < b.length; j++) {
listB.add(b[j]);
// listB.get(j);
}
int[] newA = convertToInt(listA); //convert ArrayList to Int[]
int[] newB = convertToInt(listB); //convert ArrayList to Int[]
//Merge Arrays
int al = newA.length;
int bl = newB.length;
int[] res = new int[al + bl];
System.arraycopy(newA, 0, res, 0, al);
System.arraycopy(newB, 0, res, al, bl);
// System.out.println(Arrays.toString(res));
// End of Merging
removeDups(res);
Arrays.sort(res);
System.out.println("Sort: "+Arrays.toString(res));
// System.out.println(listA);
// System.out.println(listB);
return res;
}
private static int[] convertToInt(ArrayList<Integer> listA) {
Object[] obj_arr = listA.toArray();
int[] arr = new int[listA.size()];
for (int i = 0; i < obj_arr.length; i++) {
arr[i] = (int) obj_arr[i];
}
// for (int i=0;i<arr.length;i++)
// {
// System.out.println(arr[i]);
// }
return arr;
}
public static void removeDups(int[] a){
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
// adding elements to LinkedHashSet
for (int i = 0; i < a.length; i++)
set.add(a[i]);
// Print the elements of LinkedHashSet
// System.out.print(set);
}
}
Can somebody help me?