For all ?. such \'hat O c-i-i, alilis in its,inal position in 9e winea arrav For
ID: 3700985 • Letter: F
Question
For all ?. such 'hat O c-i-i, alilis in its,inal position in 9e winea arrav For all j, such that oc-ietato.aul is sorted s true at the beginning of each a Ionly b. Il only d. Il and il only 3. The method removeDupes is intended to remove duplicates from array a, returning n, the number of eleneets in a after duplicates have been removed. For example, if array a has the values (4, 7, 11,4, 9, 5, 11, 7, 3, 5) Before removeDupes is called, then after duplicates are r returned. Explain public static int removeDupes(int [l a) int n = a.length; for(int i-o; kn; i+ int current alil int j i+1; whileicn) if(current ai aül-aln-1]: else return n How many times is the comparison (current--ail) executed in removeDupes? a. 45 b. 42 c. 36 d. 25 e. 21Explanation / Answer
The code works by comparing one element of array with all other elements of the array. When doing so if a duplicate is found then it is replaced by the last element of the array.
The 'current' variable points to the element we are checking dupicates for. 'a[j]' is the element in the array which we will compare with current to see if they are same.
If same (current == a[j]) then we move the last element (n-1)th element in place of a[j] can reduce n by1. This indicates that we have duplicated the last element at a[j] and hence we completely remove last element from our array.
The total number of times the comparision if(current == a[j]) is made is 25
When current = 4,
Comparison 1 - between 4 and 7 . No duplicate so j++
Comparison 2 - betwen 4 and 11 . No duplicate so j++
Comparsion 3 - between 4 and 4 . Duplicate so this 4 (a[j])is replaced by the last element = 5 and n--
NOTE NOW: j does not increment. Hence a[j] is now 5 (the value just placed)
Comparison 4 - between 4 and 5. No duplicate so j++
Comparison 5 - between 4 and 9. No duplicate so j++
Comparison 6 - between 4 and 5. No duplicate so j++
Comparision 7 - between 4 and 11. No duplicate so j++
Comparison 8 - between 4 and 7. No duplicate so j++
Comparison 9 - between 4 and 3. No duplicate so j++
n reached . Now i++
When current = 7
Comparison 10 - between 7 and 11 . No duplicate so j++
Comparison 11 - between 7 and 5 . No duplicate so j++
Comparison 12 - between 7 and 9 . No duplicate so j++
Comparison 13 - between 7 and 5 . No duplicate so j++
Comparison 14 - between 7 and 11 . No duplicate so j++
Comparison 15 - between 7 and 7 . Duplicate so replace a[j] with last element=3 and n--
Comparison 16 - between 7 and 3 . No duplicate so j++
n reached. Now i++
When current = 11
Comparison 17 between 11 and 5. No duplicate so j++
Comparison 18 between 11 and 9. No duplicate so j++
Comparison 19 between 11 and 5. No duplicate so j++
Comparison 20 between 11 and 11. Duplicate so replace a[j] with last element = 3 and n--
Comparison 21 between 11 and 3 . . No duplicate so j++
n reached . Now i++
Comparison 22 between 5 and 9 . No duplicate so j++
Comparison 23 between 5 and 5 Duplicate so replace a[j] with last element = 3 and n--
Comparison 24 between 5 and 3 . No duplicate so j++
n reached . Now i++
Comparison 25 between 9 and 3 . No duplicate so j++
Exit for loop.
The value of n returned is 6 which denotes the number of elements in the array after removing duplicates.
Hence whenever we found a duplicate we reduced n by 1 to denote that 1 duplicate element has been removed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.