0

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.

code image Result


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?

starball
  • 20,030
  • 7
  • 43
  • 238
  • Welcome! Please [edit] to convert your images of text into actual text. [See here](https://meta.stackoverflow.com/a/285557/11107541) for why. ['Can somebody help me?' is not an actual question](https://meta.stackoverflow.com/a/284237/11107541). Can you please ask an actual question? You can see [ask] for guidance. – starball Jan 06 '23 at 01:35
  • Why did you attach an image of your code when you also added your code as text in your post's body? – starball Jan 06 '23 at 01:35
  • 1
    Your output is showing the `toString()` method returned from an int array. – Hovercraft Full Of Eels Jan 06 '23 at 01:36

0 Answers0