SunGard Interview Question: Write, efficient code for ext... | Glassdoor

Interview Question

Software Engineer Interview(Student Candidate) Pune (India)

Write, efficient code for extracting unique elements from a

  sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3,
Answer

Interview Answer

3 Answers

4

{
int a[10]={1, 2, 4, 4, 7, 8, 9, 14, 14, 20};
int i;
for (i = 0;i<9;i++)
{
if (a[i] != a[i+1])
printf("%d\n",a[i]);
}
return 0;
}

Interview Candidate on Mar 1, 2010
0

Your version would not print out the last element, the full solution looks like this (in C#):

var arr = new[] {1, 1, 3, 3, 3, 5, 5, 5, 6, 6, 6, 9, 9, 9 };
for(var i = 0; i < arr.Length - 1; i++)
{
    var current = arr[i];
    var next = arr[i + 1];
    if(current != next){
        Console.WriteLine(current);
    }
    var isLast = i == arr.Length - 2;
    if(isLast)
    {
        Console.WriteLine(next);
    }
}

Alex on Sep 27, 2016
0

var arr = new[] { 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 6, 7, 9, 9, 9, 8 };
            for (var i = 0; i 0 && i < arr.Length - 1)
                {
                    prev = arr[i - 1];
                }
                if (current == prev && current!= next)
                {
                    Console.WriteLine(current);
                }
            }

Ghanshyam Chaudhari on Jul 19, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.