the task is :
A. Write a method public boolean single (int [] values) receiving a full array values with numbers, and returns true if the array is an organ that appears only once in the array, and false otherwise.
For example, for the array {3, 1, 4, 1, 4, 1} method returns true because that 3 appears only once, while for the array {3, 1, 4, 1, 4, 3} method returns false because there is no organ appears only once. (12%)
The method you write should be as efficient as possible. Answer is not effective enough, ie have greater complexity than that required to solve the problem will not get the full
my answer:
public boolean singel (int[] values)
{
for ( int i = 0 , i < values.length()-1; i ++)
quisort (a) ; // Time Complexity O = n(LOGn) (sorting from small to bigger)
if ( binarySearch (a, a[i])
{
if ( a[i] == a[values.length()-1])//chek if founded number is the last
//so, in that case we need to chek if number before is not the same.
//need to do free chekpoint to no pass bounds array.
if ( a[i] == a[values.length()-1])
return false;
else
return true;
if (a[i] == a[0])//if founded number first in array so need to chek only the next;
if ( a[i] == a[1])
return false;
else
return true;
if (a[i]== a[i+1])||(a[i] == a[i-1])
return false;
else
return true;
}
}
ok, that what i do n(Logn) can i do better that that?