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.

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

Students also viewed these Databases questions