1

I wanted to create a method which can check if two arrays are the same (without using any imports). Order does not matter and it can contain duplicates and the two arrays need to remain the same! My idea was to copy the first array, then compare the copy array to the second array. And if I find a valid pair, delete that item from the copy array, so it can handle the duplications. But I cannot delete any item because of a type mismatch. My code:

Solution.java

public class Solution {

    public static boolean areTheyTheSame(int[] a, int[] b)
    {

        if (a.length == b.length)
        {

            //fill the temp array with the elements of a
            int temp[] = new int[a.length];

            for (int i = 0 ; i < a.length ; i++)
            {
                temp[i] = a[i];
            }

            //check if the temp array and the b array are the same
            for (int i = 0 ; i < a.length ; i++)
            {
                for (int j = 0 ; j < a.length ; j++)
                {
                    if (temp[i] == b[j])
                    {
                        temp[i] = null; // Type mismatch: cannot convert from null to int
                    }
                    else 
                    {
                        return false;
                    }
                }
            }

            return true;

        }
        return false;
    }
}

Test.java

public class Test {

    public static void main(String[] args) {

        int[] a = new int[]{121, 144, 19, 161, 19, 144, 19, 11};
        int[] b = new int[]{121, 19, 19, 144, 161, 144, 11, 19};

        if (Solution.areTheyTheSame(a, b) == true)
        {
            System.out.println("Equal");
        }
        else 
        {
            System.out.println("Not equal");
        }

    }

}
Frigyes Vass
  • 139
  • 1
  • 4
  • 17
  • 1
    The problem is with this line: `temp[i] = null;` . Your array is an array of `int` primitives, and `int`s cannot be null. If you need nullability, consider using an array of `Integer`s. – Jordan Jan 30 '20 at 20:12
  • `int` is a primitive in Java. You can not assign a value of `null` to one. See [Why can't primitive data types be null in java](https://stackoverflow.com/questions/11047276/why-cant-primitive-data-types-be-null-in-java) for a broader explanation. – azurefrog Jan 30 '20 at 20:12

1 Answers1

3

There are a few issues there:

You are using the "primitive" int in your array. This is not an object, so it cannot be set to "null".

If you want to go that way use "Integer temp[];" (you can leave the rest as int). Java will automatically convert between int and Integer so you do not have to worry about the type conversion there. Other primitives (which cannot be "null") are boolean, long, double and float.

You are not comparing the final temp array

After you set the array, you do not check if everything is null. After adding that extra check, it should work fine.

Niko
  • 6,133
  • 2
  • 37
  • 49
  • I like how you totally skipped the fact author copies a[] -> temp[] and then compares temp[] and a[] totally ignoring b[] as well as the random return in there in else statement – Worthless Jan 30 '20 at 20:18
  • You are right. I accidentally checked the "copy" array with the "a" array, sorry... But that else statement is needed, I am sure. – Frigyes Vass Jan 30 '20 at 20:55