Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all.

I have two list boxes. One of them is filled with file names and their directories.

I want to count all duplicate file names in the first list box and put the file name and the number of duplication in the second list box.

like: 0.TXT (6)

the program:[^]

I use VC++, so please give me C++ code for I'm still a beginner. :((

Thanks in advance. :-O

check that link even it is in but you can understand the algorithem of it :

the trick is to create 2 nested loops :
// then check if(listbox[i]==listbox[k]){
then add this item to the new list ...
Share this answer
thanks a lot for the answer but I really couldn't understand many of it's expressions.

I googled it but all I found was VB code.

and about the idea you provided above, I tried it but it gives me an out of range exception.

here is what I did so far:

int L = listBox1->Items->Count::get();
int i = 0;
  for(int k=0; k<l;>  

Sorry if I were wrong!
Share this answer
DaveAuld 7-Nov-10 7:19am    
This is not an answer, use the Add Comment feature below the answer you want to comment against...........
yes sure it will give out of range I just gave an idea without giving the fulllogic ...
the trick is to start in the second loop where we have reached the last time ...

here is the logic :

for ( i=0;i<count;i++){>

for( j=i+1;j<count-i;j++){>

then do what ever you want }

Share this answer
DaveAuld 7-Nov-10 7:21am    
Do not use the Add Answer to have a discussion, use the Add Comment feature below the question or answer that you are discussing.....
aim999 7-Nov-10 8:19am    
Well, it didn't work either. Am I missing something?
for(int i=0; i<l; i++)
for(int="" j="i+1;" j<(l-i);="" j++)
if(listbox1-="">Items[i]==listBox1->Items[j]) listBox2->Items->Add(listBox1->Items[j]);
Tamer Hatoum 7-Nov-10 8:34am    
what is the error ??? can you post all of your code???
aim999 7-Nov-10 10:23am    
Yes, sure but the comment block is not enough and when I post it as an answer it gets altered
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 for(int x=0; x<=10; x++)
                 for(int y=0; y<=5; y++)
                 for(int z=0; z<=3; z++)
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                 int L = listBox1->Items->Count::get();
                 for(int i=0; i<l;>                 {
                     for(int j=i+1; j<=(L-i); j++)

It doesn't give an error and doesn't do anything too.
Share this answer
Tamer Hatoum 7-Nov-10 11:53am    
acctually it looks fine ... but can you add some breaks and debug your error for inserting some alerts in each loop to know what is happening and inform me back.. thanks...
For (i = 0 ;List.Items.Count - 2 ) {
For (j = ListItems.Count - 1 ; i + 1) {
If( List.Items(i).ToString = List.Items(j).ToString){you code here}}}
try that please...
and also try that link please I think it will help u a lot
aim999 8-Nov-10 4:13am    
- the compiler does not understand (List) in:
List <string^>^listDuplication = gcnew List();
- and (one) in:
if (one == strFile2)
-I think the other errors will be fixed if those are. I don't know why the code alters when I paste it here.
thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900