Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Remove Duplicates

Posted on 2004-04-26
21
434 Views
Last Modified: 2012-06-21
Write out a function that given a sorted list of integers would return the same set of integers with out duplicates. If you could only do 10 tests for the above function what 10 would you do?

guyz thizz not a homework question but a interview screening question i would really appreciate the answer.

thnx
krithi
0
Comment
Question by:krithi
  • 5
  • 4
  • 3
  • +3
21 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10921696
>>would return the same set of integers with out duplicates

That part holds the answer - use a 'std::set<int>', e.g.

#include <set>
using namespace std;

int input [] = { 1,1,2,2,2,3,4,4,5,6,7,8,8,8,9};

set<int> ns;

for ( int i = 0; i < sizeof (input); ++i) {

  ns.insert(input[i]);
}

int nSize = ns.size();
int* output = new int[nSize];

for ( set<int>::const_iterator it = ns.begin(); it != ns.end(); ++ii) {

   output[i] = *it;
}



0
 
LVL 86

Expert Comment

by:jkr
ID: 10921703
Oops, the last loop should be

for ( set<int>::const_iterator it = ns.begin(); it != ns.end(); ++it) {

  output[i] = *it;
}
0
 

Author Comment

by:krithi
ID: 10921965
thnx but can u just write this in simple c or c++.

thnx
krithi
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
jkr earned 43 total points
ID: 10922091
>>thnx but can u just write this in simple c or c++.

This is the C++ area, and the above is simple C++...
0
 

Author Comment

by:krithi
ID: 10922221
I meant withou using STL
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10922259
That is simple C and shouldn't be posted here in C++:

#include <string.h>

        int input [] = { 1,1,2,2,2,3,4,4,5,6,7,8,8,8,9};

        int last = input[0];
        int siz  = sizeof(input);
        for (int i = 1; i < siz; i++)
        {
             if (input[i] == last)
             {    
                  if (i < siz-1)
                     memmove(&input[i], &input[i+1], siz-i-1);
                  input[--siz] = 0;                
             }
             last = input[i];
        }

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10922280
Should be

            }
            else
               last = input[i];

Regards, Alex
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 41 total points
ID: 10922323
>>  If you could only do 10 tests for the above function what 10 would you do?

I would ask jkr, AlexFM, Axter, DanRollins, Sys_Prog, bcladd, STeH, chensu, rstavely, khkremer to check my function and wonder how i could make so many mistakes in such a little function.
0
 

Author Comment

by:krithi
ID: 10922406
will this work?

void RemoveDuplicates(struct Node* pHead)
{
      //pHead - Beginning of the sorted list.
      if (pHead)
      {
            struct Node* pCurrent = pHead->pNext;
            struct Node* pPrev = pHead;
            // add pPrev->nValue to the new sorted list with no duplicates.
      }
      
      while(pCurrent)            // iterate the list till the end
      {
            if(pCurrent->nValue == pPrev->nValue)
            {
                  // move to current pointer to the next node.
                  pCurrent = pCurrent->pNext;
            }
            else
            {
                  //add pCurrent->nValue to the new sorted list with no duplicates.

                  // move the previous pointer to the current node.
                  pPrev = pCurrent;                  

                  // move to current pointer to the next node.
                  pCurrent = pCurrent->pNext;            
            }
      }
}
0
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 41 total points
ID: 10922581
Only problem I detect is that it won't include the first value in the result list. So the result is empty for a single valued list.

-bcl
0
 

Author Comment

by:krithi
ID: 10922938
i think this will do the job

if (pHead)
     {
          struct Node* pCurrent = pHead->pNext;
          struct Node* pPrev = pHead;
          // add pPrev->nValue to the new sorted list with no duplicates.
     }
0
 
LVL 11

Expert Comment

by:bcladd
ID: 10923923
Then follow that with your while loop and you are good to go.

What test cases would you use (and why)?
0
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 10925990
but i thought he says 10 tests?
how do you use only 10 tests for a general problem?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 10927436
The point of limiting you to 10 tests is two-fold:
  -- Even with automated testing, writing test cases with more than 10 different test scenarios for a routine of this length is probably a waste of effort (if the test cases are well chosen)
  -- Regardless of the number of tests, you can easily prioritize what you want to test and come up with 10 tests that will catch many bugs (off by 1 errors, null input errors, etc.)

You ask how 10 tests oculd work for a general problem. How many tests would you run? How many tests would your employer want to pay you to run? The point is that with well chosen test cases you can get good coverage (of the infinite logical input space).

-bcl
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 10927932
#include <algorithm>

int input [] = { 1, 2, 2, 3, 3, 3 };
int output[3];

copy(input, unique(input, input + sizeof(input)), output);
0
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 10927966
i see thanks, bcladd.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10928075
I think it's a maximum of 4, 5 scenarios that should be run for that limited complexity:

1. An empty list and one having only one element
2. A list that had no duplicates
3. A list that has only one value
4. A list that has a few duplicates (normal case)
5. A list that has many duplicates, one at begin and one at end

Regards, Alex


0
 

Author Comment

by:krithi
ID: 10934121
thnx
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question