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
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... -
string[] ss ={ "1","1","1"};
-
List<string> myList = new List<string>();
-
-
-
foreach (String s in ss)
-
{
-
if (!myList.Contains(s))
-
{
-
myList.Add(s);
-
}
-
}
-
-
foreach (String s in myList)
-
{
-
Console.WriteLine(s);
-
}
-
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
PRR 750
Recognized Expert Contributor @cathy25
You will have to explain more... post some sample code....
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.
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 ....
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: - string[] nonDuplicateArray = new string[16];
Then fill it, checking for duplictes: - for(int i=0; i<16; i++){
-
// For each item in original array, if my new array already has it,
-
// make it a blank, otherwise, copy it to the new array
-
if(Array.IndexOf(nonDuplicateArray, FirstItemId[i]) > -1)
-
nonDuplicateArray[i] == "";
-
else
-
nonDuplicateArray[i] = FirstItemId[i];
-
}
now return your second array. - return nonDuplicateArray;
Yes! it worked. Thanks mldisiblo.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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
|
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
|
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
| |
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
|
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
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |