Hash Flashcards

1
Q

When adding all elements from an array to a hash? Why can’t you use the hash.addAll function?

A

Because it treats the primitive array as one big object. To add everything from an array to a hash, you must use a typical for loop iterator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use a HashSet over a linked list?

A

-Linked List have a slow lookup/search
- arrays don’t have random access so accessing an element takes O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why use a boolean array?

A

We use a boolean array as a hashset that marks if we’ve seen that value before.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the Hash do to a String?

A

Returns an integer

For example, if we use “Nick” it might return 4 to represent the length of the name

Then we pass it to the 4th spot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Hash Function

A

A function that takes an input and retunrs an integer based on that input’s value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Hash Code

A

The value returned by a Hash Function that determines the numeric mapping of this element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HashSet

A

A data structure in which elements are placed in their respective array locations based on their hash code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HashSet Add

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

HashSet Remove

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

HashSet Contains

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to deal with collisions in a hashset?

A
  • ## Open Addressing: Just put it in the empty spot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does contains work in a hashset?

A
  • Get the location through the same hash method and see if its there
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What if the contains can’t find it in our hashset?

A
  • Get the location, if we don’t see it, keep going through the list until we find it or if we find a null, then return false because then its impossible to be in our hashset
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly