Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Removing Duplicates You are given an array of n elements, and you notice that some of the elements are duplicates; that is , they appear

Removing Duplicates
You are given an array of n elements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Provide an algorithm to remove all duplicates from the array. (Note that any sorting is accomplished using the merge sort algorithm.)
Consider the following algorithms and pick any that would receive full credit in this class. There may be more than one such answer.
For simplicity we will consider removal from the array to be O(1) since we can just as easily write the solution to a new array:
a.
For each item in the array, we compare it to all other items in the array and if we see more than one, remove the additional occurrences and return the final array as our solution.
b.
We use a hashset and store each element in the set as we see it. If it already exists in the set then it won't be added again so any duplicates will automatically be ignored. Once we are done, return the elements in the set as an array.
c.
We sort our array with merge sort and then linearly scan the sorted array to remove any elements that are identical to the previous element. Returning the final array with duplicates removed.
d.
sorted_array = merge_sort(input_array)
for i =1->n-1:
if sorted_array[i]== sorted_array[i+1]:
remove(sorted_array[i])
return sorted_array
e.
We run a modified merge sort such that each merge step will check if left and right are the same, if they are then discard the right element and continue the merge step as normal. Return the sorted array with duplicates removed... Let's find the matching runtime analysis.
Note that while some final runtimes are the same, we are looking for the one that matches each solution... 1. For each item in the array... 2.We use a hashset and store each element...3. We sort our array with merge sort and then linearly scan...4. sorted_array = merge_sort(input_array)...5.We run a modified merge sort... Answer key: O(n^2), O(n log n), The linear scan takes O(n) so the overall runtime is O(n) beating the target O(n log n)., It takes O(n log n) to sort an array with merge sort, and then a linear scan to remove duplicates takes O(n) total, so the final runtime is dominated by the O(n log n) merge sort runtime., merge_sort()= O(n log n); for i =1->n-1= O(n); O(n log n)+ O(n)= O(n log n), O(n), Hashsets in this class is considered O(n) worst case for all operations. So adding n elements to a hashset takes O(n^2) time., The target runtime is O(n log n) so my correct solution would have that runtime obviously., We don't need runtimes for D&C solutions., We add to the mergesort comparison step which is O(1) with another O(1) step so the overall runtime is unchanged giving us O(n log n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions