• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion on HashSet

 
Anupam anand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I am confused how its output is 2 1 1. Please anyone explain it. Thanks in advanced.

[Edit: added code tags - MB]
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you expect the output to be?

You add two numbers. You add a duplicate, but this is a Set, so it doesn't allow duplicates. You remove one of them. You then try to remove a different value, so that has no effect.
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes perfect explanation by Metthew Brown.

If you use List instead of set like


Then you will get 3 2 2... That might be your expectation...
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Mathew,

I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?

Matthew Brown wrote:What would you expect the output to be?

You add two numbers. You add a duplicate, but this is a Set, so it doesn't allow duplicates. You remove one of them. You then try to remove a different value, so that has no effect.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prince Sewani wrote:But Mathew,
I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?


The set does not contain 47. So the command "set.remove(47)" has no effect. The set is unchanged and its size is still 1.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dennis,

But the set contains i2 i.e. 46 right??


Dennis Deems wrote:
Prince Sewani wrote:But Mathew,
I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?


The set does not contain 47. So the command "set.remove(47)" has no effect. The set is unchanged and its size is still 1.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prince Sewani wrote:But the set contains i2 i.e. 46 right??

The set contains 46, which used to be the value of i2. But it isn't any more - you assigned i2 to a different object.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah,Sorry I missed that one out..

Matthew Brown wrote:
Prince Sewani wrote:But the set contains i2 i.e. 46 right??

The set contains 46, which used to be the value of i2. But it isn't any more - you assigned i2 to a different object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic