In this video we're going to look at how to handle duplicates.
So let's say we have an entry at 104 with Flop Smith.
And we don't know that there is a flop Smith at 104 at this key.
And by accident we somehow add an employee here.
So employees dot add and we say at 104 we want to add Mike JK.
All right.
So if we now do this let's run it.
We will see that we will run into an error.
So it says system dot argument exception.
An item with the same key has already been added.
So you can see you cannot add a new item at the same key at which an item already exists.
So how do you potentially fix that?
Or how do you get your way around it?
Well, one would be to do if you're not sure that an item at this particular key exists or not, you
can just use this approach here.
So instead of saying add Mike at 104 or Mike JK, what we could have done is we could have just said
add 104.
So at this key of 104 enter Mike JK okay, but this would be updating right.
So this would not be adding.
But what if we actually want to make sure that we add an item.
Well the thing is what we can do is we can check if the item already exists.
So we can go ahead and say if employees dot contains key.
And then we need to say which key we are looking at.
So if there is a key at 104, then we're not going to add the item.
However, we can also add the exclamation mark and we can say if there is no item at this key of 104.
So employees does not contain an item at this key 104, then go ahead and say employees dot add and
then add at 104 your Mike Jake.
Okay, I'm very creative with the names as you can see.
So yeah there we are.
So that's how you can handle duplicates.
You can check whether an item exists and then you can add it if you want to.
Otherwise like you would need to find the ID that is still free and you would need to add it there.
Now this would be basically a while loop where you go through all keys that exist or don't exist and
you for example, just increment those IDs up to the point where the key is free, and then you would
just add the new item at that key position, specifically at this key specifically.
So this would be a way to handle it okay.
So we create a counter.
We use this while loop where we just say okay.
As long as at this counter position there is an employee just increment counter.
And then at the point where this is not the case anymore, this while loop would finish and we would
get to this line here where we just say, okay, add that counter position.
Whatever the position is, add Jesus Christ.
Okay, so this is just a small workaround that you could use for that specific situation.
Now another way would be so this is really just a workaround.
So let me get rid of this.
If you want to add an employee at a given ID but you're not sure whether you can, you can use the try
add method.
So you can use employees dot try add.
And what this try add will do is it will return a bool, and it will try to add an item to your dictionary
at a given ID.
So let's say if I want to add somebody at 102 and I give it the name of Michael Brims, then this will
only work if at 102 there is no one.
So if this spot is still available.
So I can just create a bool which will say add it.
So for example, if this worked, so if adding worked then this will be true.
Otherwise it will be false.
So I can just check if added is not true then I'm going to say could not add the you.
The employee or employee already exists.
So employee with the ID of 1 or 2 already exists.
So if we run this we will see that this does actually already exist.
So employee with ID 1 or 2 already exists and that is Jane Smith.
Okay.
And why do we know Jane Smith.
Well because we used this for each loop to display all users that we have or employees that we have
in our dictionary.
Okay.
So these were just a few things that you can do with dictionaries.
I would say let's look at the dictionary where the key is still an int but the.
Value is more complex than just a string.
All right, so see you in the next video.