top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Fatest way to remove duplicate elements from a sorted array ?

+1 vote
256 views
Fatest way to remove duplicate elements from a sorted array ?
posted Oct 9, 2013 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

It can be done in O(n)

void dedup(vector<int> &v)
{
    int i = 0;
    for(int j = 1; j < v.size(); ++j)
    {
        if(i != j)
        {
            if(v[i] != v[j])
            {
                v[++i] = v[j];
            }
        }
    }

    v.resize(i+1);
}
answer Oct 9, 2013 by anonymous
Similar Questions
+5 votes

Input: {5, 8, 5, 3, 3, 2, 7}
Output: {5, 8, 3, 2, 7, ?, ?}

Idea is that we should remove the duplicate number from the array and should return the array in the original form (obviously without duplicate).

Please help me with an efficient algorithm or C code.

+2 votes

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).

0 votes

Write a c program that rotate elements of an array by the value of configured number "n".
For example:
Input array[ ] = { 2, 3, 5, 6, 8, 9}
value of n = 2
Output array[] = { 5, 6, 8, 9, 2, 3}
I am looking for efficient program.

...