469,590 Members | 2,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

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 SearchDuplicateEntry {

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

catch(Exception e)
{
Console.Writeline("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 13258
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 SearchDuplicateEntry {

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

catch(Exception e)
{
Console.Writeline("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.Length; 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.ContainsKey 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.com>
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.ContainsKey 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.ContainsKey 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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Joseph Lee | last post: by
3 posts views Thread by shine | last post: by
11 posts views Thread by garyhoran | last post: by
2 posts views Thread by christophe.leroquais | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.