473,800 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find the duplicate items in a list?

21 New Member
Hi,
My requirement is to check for duplicate items in a list of 16 strings.They will be entered by users like this...Item1,It em2,Item3....It em16. 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
7 23047
PRR
750 Recognized Expert Contributor
@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
cathy25
21 New Member
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.t ext
}

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

TIA
Dec 29 '08 #3
PRR
750 Recognized Expert Contributor
@cathy25
You will have to explain more... post some sample code....
Dec 29 '08 #4
cathy25
21 New Member
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....,Syst em 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
PRR
750 Recognized Expert Contributor
@cathy25
Instead of using string array[] "FirstItemI d" use List<String>... that way you wont have duplicate items....
Put your "Item1,Item2,It em3....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
mldisibio
190 Recognized Expert New Member
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
cathy25
21 New Member
Yes! it worked. Thanks mldisiblo.
Dec 29 '08 #8

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

Similar topics

5
4506
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's my function: +++++++++++++++++++ from sets import Set
10
3305
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
5
8932
by: mamatha | last post by:
Hi I am developing a VB.NET application,in that application i have a listbox.When adding items to the listbox some duplicate items are added,how can i prevent listbox items from duplication.If any one knows please suggest me. Mamatha
4
2838
by: gurvar | last post by:
Hi I'm trying to remove duplicate elements from a Drop Down List Fill in VB.net. Following code worked well with vb6. But I'm getting index out of range if I try to translate it to vb.net code at line#4. Any possible clues will be appreciated. Thanks. 'To distill the Combo Element NumY = Combo5.ListCount + 1 '''''''''''''''Line1 For NumX = 1 To Combo5.ListCount '''''''line2
4
3634
by: Shi Mu | last post by:
I used the following method to remove duplicate items in a list and got confused by the error. >>> a , , ] >>> noDups='] ] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: iterable argument required
3
1866
by: Jim McGivney | last post by:
In VWD I have an aspx page with a DropDownList control. The DropDownList is populated from a column from a table in an Access database. If there are duplicate values in the column they are added to the DropDownList. Is there a way to eliminate duplicate items on the DropDownList ? Thanks, Jim
10
6806
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter through from a combo box. The combo box items are made up of IP address and computer host name. Anything a user places in this combo box it writes this to a txt file called history.txt
1
3486
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying (though not critical) problem, duplicate stories. Apparently there is overlap with some of the sites I subscribe to so I get duplicate stories. Does anyone know of some sort of filter (software or online service) that can remove duplicate stories? Any...
2
2538
by: Harry Haller | last post by:
I want to duplicate the form data, edit it to remove some items (such as __EVENTTARGET, __EVENTVALIDATION, etc) and save it to a log. How can I make a duplicate (editable) copy of the Form Collection? The Form Collection is of type NameValueCollection is, underneath the covers, an object of type HttpValueCollection. The problem I have is that my copies have only one key item for a list (such as a radio button list) whereas the Form...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.