Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is O(n) of the method: removeDuplicates? Assume the major operation is the

ID: 669136 • Letter: W

Question

What is O(n) of the method: removeDuplicates? Assume the major operation is the comparison operation.

private static int[] removeDuplicates(int[] s){
   int end = s.length;

   for (int i = 0; i < end; i++) {
   for (int j = i + 1; j < end; j++) {
   if (s[i] == s[j]) {
   int shiftLeft = j;
   for (int k = j+1; k < end; k++, shiftLeft++) {
   s[shiftLeft] = s[k];
   }
   end--;
   j--;
   }
   }
   }

   int[] newlist = new int[end];
   for(int i = 0; i < end; i++){
   newlist[i] = s[i];
   }
   return newlist;
        

Explanation / Answer

The function looks fine to me. I've written inline comments. Hope it helps:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote