Describe an efcient recursive method for solving the element

Describe an efcient recursive method for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.

Solution

The problem can be solved in O(n^2) using two for loops in cascade which gives the function an O(n^2) without the usage of sorting of elements

Algorithm:

The algorithm will return \"1\" or true if the elements in the array are unique and return \"0\" if the elements are not unique.

A is the input array containing the elements from index 0 to (n-1)

for ( i= 0 ; i <= (n-1); i++)

{

      for(j=i+1;j<=(n-1);j++)

           {

                   if(A[i]==A[j])

                            return 0;

                            break

           }

}

return 1;

Describe an efcient recursive method for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sortin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site