By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

How to find the duplicate items in a list?

P: 21
Hi,
My requirement is to check for duplicate items in a list of 16 strings.They will be entered by users like this...Item1,Item2,Item3....Item16. I need to check for the repeated items if any exists and need to reset it to zero or null.
Could any body point me in a direction to write the logic in C#?

Thanks,
Cathy
Dec 28 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 750
PRR
@cathy25
For any list look for Items.Contains method ...If Contains() returns false you can add else no...
Expand|Select|Wrap|Line Numbers
  1. string[] ss ={ "1","1","1"};
  2.             List<string> myList = new List<string>();
  3.  
  4.  
  5.             foreach (String s in ss)
  6.             {
  7.                 if (!myList.Contains(s))
  8.                 {
  9.                  myList.Add(s);
  10.                 }
  11.             }
  12.  
  13.             foreach (String s in myList)
  14.             {
  15.                 Console.WriteLine(s);
  16.             }
  17.  
Dec 29 '08 #2

P: 21
Thanks for your reply. But, in my case, I don't have any lists.
we are loading the user input as follows

for(int i=1;i<=15;i++)
{
string FirstItem=ID1.text
}

...like this. Then how can I use this in my foreach statment?

TIA
Dec 29 '08 #3

Expert 100+
P: 750
PRR
@cathy25
You will have to explain more... post some sample code....
Dec 29 '08 #4

P: 21
for (int i = 1; i <= 15; i++)
{
string firstId = FirstItemId[i].ToString();
string secondId = FirstItemId[i+1].ToString();
if (firstId == secondId)
{
MessageBox.Show("Duplicate Item exists....,System is resetting it to blank! If you would like to change it, open the form again");
SecondItemId='';
break;
}
I know this code checks every 2 items sequentially and returns the result. But, I need to check all the 16 items for duplication.
Dec 29 '08 #5

Expert 100+
P: 750
PRR
@cathy25
Instead of using string array[] "FirstItemId" use List<String>... that way you wont have duplicate items....
Put your "Item1,Item2,Item3....Item16" in list<string> .. use Contains() method to see if the value already exists...Refer the earlier code i posted above ....
Dec 29 '08 #6

Expert 100+
P: 190
I believe cathy25 wants to maintain an array of 16 items, but have any duplicates blanked out.
You can follow DeepBlue's logic slightly modified:

You have your first array already, FirstItemId which is string[16].
Make a second, empty string array:
Expand|Select|Wrap|Line Numbers
  1. string[] nonDuplicateArray = new string[16];
Then fill it, checking for duplictes:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<16; i++){
  2.   // For each item in original array, if my new array already has it, 
  3.   // make it a blank, otherwise, copy it to the new array
  4.   if(Array.IndexOf(nonDuplicateArray, FirstItemId[i]) > -1)
  5.     nonDuplicateArray[i] == "";
  6.   else
  7.     nonDuplicateArray[i] = FirstItemId[i];
  8. }
now return your second array.
Expand|Select|Wrap|Line Numbers
  1. return nonDuplicateArray;
Dec 29 '08 #7

P: 21
Yes! it worked. Thanks mldisiblo.
Dec 29 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.