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:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.