473,805 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicate keys in Hashtable

I am trying to add an integer array to a hashtable.
While adding to hashtable I want to check the duplicates in array, the
way I want to do is add the key(the integer stored in array) if a
duplicate key is being added then the program should return the integer
else it should return -1.

Here is the piece of code I have written does the logic looks ok ?
------------------------------------------------------------------
class SearchDuplicate Entry {

int[] ArrayOfInt = new int(N);
int Length = ArrayOfInt.leng th;
int Count = 0;
Hashtable h = new hashtable();
try
{
for(int i=0; i<Length; i++)
{
Count++;
h.Add(ArrayOfIn t[i],Count);
}
}

catch(Exception e)
{
Console.Writeli ne("ArrayofInt[i]");
}

return -1;
}

I want to know if I am thinking in right direction.
Any help will be greatly appreciated, thanks so much.

Sep 1 '05 #1
3 13889
shine <su***********@ gmail.com> wrote:
I am trying to add an integer array to a hashtable.
While adding to hashtable I want to check the duplicates in array, the
way I want to do is add the key(the integer stored in array) if a
duplicate key is being added then the program should return the integer
else it should return -1.

Here is the piece of code I have written does the logic looks ok ?
------------------------------------------------------------------
class SearchDuplicate Entry {

int[] ArrayOfInt = new int(N);
int Length = ArrayOfInt.leng th;
int Count = 0;
Hashtable h = new hashtable();
try
{
for(int i=0; i<Length; i++)
{
Count++;
h.Add(ArrayOfIn t[i],Count);
}
}

catch(Exception e)
{
Console.Writeli ne("ArrayofInt[i]");
}

return -1;
}

I want to know if I am thinking in right direction.
Any help will be greatly appreciated, thanks so much.


Okay, problems with the above:

1) You need code to be in a method, not just directly in the class
body.

2) Most people don't use camel case for parameters and local variables
- use it for properties, method names and type names.

3) C# is case sensitive - you need new Hashtable(), not new
hashtable(). (It's always worth posting code which actually works if
possible.)

4) Why do you have the local variable "Length"? Why not just do

for (int i=0; i < ArrayOfInt.Leng th; i++)
{
....
}

or even better

foreach (int i in ArrayOfInt)
{
....
}

5) You shouldn't be catching an exception here - what are you expecting
to go wrong, and why do you think you shouldn't let that exception
propagate?

6) It's not at all clear what the "bigger picture" is here - are you
*just* trying to find duplicates, and just the first duplicate at that?
If so, I wouldn't bother putting the value as "count" - I don't see any
benefit. Just test using Hashtable.Conta insKey before adding the key
(returning the key if it's already present instead) and then set the
value for the key to be null (or some other constant reference -
there's no point in boxing something you're not going to use again).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 1 '05 #2
Thanks Jon for the reply.
I am trying to find the duplicate entries.What I would like to know is
what is the complexity of Hashtable.Conta insKey method.I want to
achieve a real fast search.

Let me make sure that I understood what you mean:
copy the array in hashtable,while adding keys and value check if the
key has already been added.If yes return some value else continue
adding.

Am I correct in my understanding?
sorry about the case sensitivity I just wrote the code in notepad just
to develop the logic so no intellisense :-).
I am catching the exception because if I add a duplicate key to
hashtable it will throw an exception.

thanks again Jon.

Sep 1 '05 #3
shine <su***********@ gmail.com> wrote:
Thanks Jon for the reply.
I am trying to find the duplicate entries.What I would like to know is
what is the complexity of Hashtable.Conta insKey method.I want to
achieve a real fast search.
It's fast. I wouldn't worry about just how fast until you've got some
evidence that it's a bottleneck for you.
Let me make sure that I understood what you mean:
copy the array in hashtable,while adding keys and value check if the
key has already been added.If yes return some value else continue
adding.
Yes.
Am I correct in my understanding?
sorry about the case sensitivity I just wrote the code in notepad just
to develop the logic so no intellisense :-).
I am catching the exception because if I add a duplicate key to
hashtable it will throw an exception.


That means you're using the exception as flow control - not a good
idea. If you use ContainsKey instead, you don't need any exception
handling, and it'll be clearer what you intend to do.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 1 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10131
by: faustino Dina | last post by:
Hi, I need to store my data on a sorted collection, where the sorting key can be duplicated. I've been looking around .NET collection classes but they expects unique keys for sorting. What can I do? Is any other resource for ..NET collections on the web? Thanks in advance --
7
6892
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
5
1510
by: ad | last post by:
I have a hashtable name myHash. sKey is a string, I use myHash to retrieve the value of that key. But is sKey is not in the myHash.Keys, it raise an Exception. I want to dertiminate if sKey in myHash.Keys. How can I do that?
2
10973
by: Terry Burns | last post by:
I want to use a collection of value pairs but dont want to be restricted to unique keys. Does anyone know how I can do this. I dont want to use an array because I need the flexibility of a collection to allow growing or shrinking lists. Regards
4
5895
by: Neil Wallace | last post by:
Hi there, I have an application in which a grid of 100 or more buttons are put on a form in columns of 10. All the buttons are within a panel. They are added in runtime, and so they adopt a sensible tab value. The tab key moves the focus down the column one by one, and the up and down arrow keys work well.
3
981
by: shine | last post by:
I am trying to add an integer array to a hashtable. While adding to hashtable I want to check the duplicates in array, the way I want to do is add the key(the integer stored in array) if a duplicate key is being added then the program should return the integer else it should return -1. Here is the piece of code I have written does the logic looks ok ? ------------------------------------------------------------------ class...
14
1875
by: Ron M. Newman | last post by:
Hi group, I need similar functionality I'm getting from the hashtable, but without the value .all I need is keys. this of course negates the need for "hashing", but I wanted to know if there's a class for that in C#. I need to: - Set keys - look for the existence of keys
11
7810
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the application ie sometimes I want to cycle through the values in the collection in DateTime order while at other times in TimeSpan order. Ideally I would like multiple keys - such as Timespan within DateTime order but maybe that is asking too much.
2
2005
by: christophe.leroquais | last post by:
Hi, I have a HashTable that can have the same keys. For example: Key: myKey1, myKey2, myKey3, myKey1 Value: "value1" , "value2" , "value3", "value4" That's ok and I'm happy with that. My issue is that when I want to retrieve the entries one by one, I want
0
9718
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
10614
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
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.