I am unsure as to why my code does not give the correct outputs for the test data which has been provided to me.
Write a procedure 'allDistinct' that takes an array of objects and returns true iff they are all distinct (no two identical).
Your procedure must work for all classes, You may type it using a variable type T
<T> boolean allDistinct(T[] a)
{
boolean r = true;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a.length; j++)
{
if (a[i] == a[j])
{
r = false;
}
}
}
return r;
}
I have tried using .equals()
instead of ==
but this resulted in no change and I am not sure why it is still incorrect.