Question

In: Computer Science

You are given an array of n elements, and you notice that some of them are...

You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

A simple modification to merge sort will do the trick. Merge sort is O(n log n). It's also stable, meaning that items with equal keys will remain in their same relative order. The only difference here is that you want to eliminate duplicates. The only change to the code is in the comparison and copying of items.

You would modify the code so you don't copy equal items:

while (leftIndex < leftMax && rightIndex < rightMax)
{
    if (a[leftIndex] < a[rightIndex])
    {
        output[outputIndex] = a[leftIndex];
        ++leftIndex;
    }
    else if (a[leftIndex] > a[rightIndex])
    {
        output[outputIndex] = a[rightIndex];
        ++rightIndex;
    }
    else
    {
        // items are equal.
        // increment the right, but don't copy anything
        ++rightIndex;
    }
}

You could also do this with a standard merge sort and then do a final pass over the sorted array to remove duplicates.

Kindly revert for any queries

Thanks.


Related Solutions

You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide java code...
You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide pseudocode (iii)...
Consider the element uniqueness problem: check whether all the elements in a given array of n...
Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct answer in pseudo code places
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with each element between 1 and N, write a program to determine whether there are any duplicates. //You must prompt the user for the array elements. //Display the contents of the array, along with the values that are duplicated and how many times they appeared in the array. //NOTE: N should be at least 15. Input Validation: Verify that each element entered has a value...
Let A[1 · · · n] be an array of n elements and B[1 · ·...
Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that m ≤ n. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ i ≤ m. For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten...
Let A[1 · · · n] be an array of n elements and B[1 · ·...
Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that m ≤ n. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ i ≤ m. For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
Given the following array of 8 elements, show the steps to sortthe elements in ascending...
Given the following array of 8 elements, show the steps to sort the elements in ascending order using a radix sort. Fill in each of the following blanks.81   546   677   9   97   12   53   22Adjust with 0s:Buckets for ones digit:Buckets for tens digit:Final Result:
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
You are given an array of length n that is filled with two symbols (say 0...
You are given an array of length n that is filled with two symbols (say 0 and 1); all m copies of one symbol appear first, at the beginning of the array, followed by all n − m copies of the other symbol. You are to find the index of the first copy of the second symbol in time O(logm). include: code,  proof of correctness, and runtime analysis  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT