Questions tagged [hashset]

A HashSet encapsulates operations that allow for the comparison of elements in collections. HashSets are frequently used to determine overlapping and unique elements within a collection.

HashSet<T> was introduced in .NET Framework 3.5 as part of the System.Collections.Generic namespace. HashSet is an unordered collection containing unique elements and provides a set of standard set operators such as intersection and union (plus many more). It has the standard collection operations Add (although this method returns a Boolean indicating whether or not that element already existed in the collection), Remove, and Contains, but because it uses a hash-based implementation for object identity, these operations are immediately accessible without looping the entire list as occurs with the List<T> collection for example (O(1) rather than O(n)).

Source: Linq to Objects using C# 4.0, Troy Magennis, Addison Wesley, 2010, Pearson Education Inc, ISBN-13: 978-0-321-63700-0

References

2277 questions
0
votes
1 answer

Create unique HashSet of objects

I created a class Person and a HashSet. If I add the same person to the HashSet, it is not aware the Person is already present and it will add the same person multiple times. What function do I need to overwrite to have only unique elements in the…
Nick_F
  • 1,103
  • 2
  • 13
  • 30
0
votes
7 answers

Is there a better way to add several words to a Set at once?

I was just wondering, what is the best way to add several items to a HashSet at once? I'm working on a homework assignment where the object is to iterate through a .java file and count the keywords in the file. At the bottom of the assignment…
snowBlind
  • 955
  • 2
  • 10
  • 19
0
votes
1 answer

Concise way to get the single element contained in a HashSet, C#

I know for sure that at a particular point in my program a HashSet I've constructed will only contain a single element. I know I can get the element by doing this: foreach (int num in myHashSet) { return num; } But I don't like the idea of…
Adam
  • 8,752
  • 12
  • 54
  • 96
0
votes
1 answer

Clojure set and hash-set returns unordered value

I'm playing with Clojure recently and I've had encountered some tricky things there. Why does: (set [3 3 3 3 4 "Sample String"]) returns in my REPL: ;=> #{4 3 "Sample String"} When it comes as natural that is should return: ;=> #{3 4 "Sample…
user6951244
0
votes
2 answers

Find the intersection of two strings in which characters appearing in both strings are returned (same sequence order as 1st String)

I am using HashMap() for the problem but am facing issues with regards to order and occurrence of characters in output. I tried to reverse the String builder both while iteration and after StringBuilder was created, still face another issues. int l1…
0
votes
1 answer

Array looping and comparing if >2 differences

I'm trying to check the difference between a master list of items in c# vs an array of lists. Not quite sure how to build this logic in an efficient way. Example: My master list of items is: var MasterOrderIDs = {1,2,3,4} Then I have an…
0
votes
2 answers

How to merge to HashSets with partially equal Objects?

I have an ArrayList data containing Objects of the type Personwhich updates every n seconds and has the total amount of existing data. To display this data in a table i used to clear() an ObservableList and used addAll(data) to avoid GUI hickups. I…
0
votes
1 answer

Key, Value, Hash and Hash function for HashTable

I'm having trouble understanding what the Hash Function does and doesn't do, as well as what exactly a Bucket is. From my understanding: A HashTable is a data structure that maps keys to values using a Hash Function. A HashFunction is meant to map…
K.Doe
  • 39
  • 1
  • 9
0
votes
0 answers

get() and contains() methods do not find object in HashSet although it can be found by iterating and using equals() and the hashcode is the same

I have a program which first generates a Hashmap with all allowed instances of a particular object, called BoardState, as the keys. I then iterate over the keyset, creating copies of the BoardState objects and performing transformations on them and…
OhFudgeIt
  • 1
  • 1
0
votes
0 answers

Implement an efficient data structure to manage a birthday wishing system

In an interview I was asked this question, You are the college principal and want to send a birthday wish to all the students who have their birthdays today. How do you implement this? I said I will go with a Multi Hashset of dates where chaining is…
Shubham Patel
  • 133
  • 2
  • 7
0
votes
2 answers

contains() method in java.util.HashSet doesn't behave as i expected from it

This is the java main() method: public static void main(String[] args) { HashSet set = new HashSet(); Mapper test = new Mapper("asd", 0); set.add(test); System.out.println(new Mapper("asd",…
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
0
votes
1 answer

Kotlin HashSet wrong behaviour (bug)

I've got a strange behaviour of the HashSet class. I don't know why, but method contains returns wrong result. Here an image from debugger: rootElemetns is a HashSet that contains some FsEntries that are represented by a simple data class (without…
don11995
  • 715
  • 6
  • 18
0
votes
1 answer

Java HashSet doesn't add two objets with same hashCode and equals (OK) but contains() says second object is not in set

I have a test for testing that adding the same Edge (Arista) but with the same vertices (but flipped order) is the same (this is not a directed graph). And this is strange because the two first assertions passes OK (adding Edge1 and Edge2 will…
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
0
votes
2 answers

Returning String of joined certain fields from objects in HashSet

I'm trying to write getter for a HashSet, where Object contains multiple attributes of Strings and Integers. Getter should return joined String of only two of the String fields, separated with coma, for every object that is stored in HashSet. I…
0
votes
2 answers

Two Sum Problem with HashSet not working with duplicates

I am working on an assignment where I have to find pairs of numbers summing up to "x" with average/best O(n) or linear runtime complexity. I can not use brute force as it will increase the complexity. I am using HashSet and using contains method I…
Prasanna
  • 79
  • 2
  • 11