Solved

C++, sorting an array and dumping duplicate values

Posted on 2004-09-27
3
417 Views
Last Modified: 2010-05-18
I have an array that was bubble sorted alphabetically.  Now I have the array but have duplicate values.  I want to print out the array but not print the duplicate values.  We are about 4 weeks into the course so I'm hoping the solution is not too complex
0
Comment
Question by:police45s
3 Comments
 
LVL 2

Accepted Solution

by:
pek99 earned 125 total points
ID: 12164721
when dumping the array of strings, remember the pointer of the previously printed one, make strcmp, or other string
comparation with the current string and print only when it is different.
0
 
LVL 4

Expert Comment

by:avi247
ID: 12165161
Heres a pseudo code..assuming the array is sorted.


   i = 0
   j = i + 1

 For loop = 0 to ARRAY_SIZE {
    If (arr(j) != arr(i)){
      Print arr(i)
      i = j        
    }
     j++      
  }
 
0
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12225619
int nArraySize = 250;

int* pArray = new int[nArraySize]; //the array with some values

// your sorting goes here

int nOldValue;
int nNewValue;

for( int i=0 ; i<nArraySize ; i++ )
{
   nNewValue = pArray[i];
   bool bWrite = (i==0); //first value is always written

   if( bWrite==false ) //if not the first, check if new value is the same
   {
      bWrite = (nNewValue!=nOldValue);
   }

   if( bWrite )
   {
      cout << nNewValue << endl;
   }

   nOldValue = nNewValue; //remember last value written
}
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 77
Reading variable length EBCDIC in SAS 9 79
VBA color chart bars 12 67
groovy example issue 10 71
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now