Learn

The hashing function is the secret to efficiently storing and retrieving values in a hash table. A hashing function takes a value, passes it through a formula, and produces a result called a hash.

This function is said to be deterministic. That means the hashing function must always return the same value when given the same key. This is important because we will need to hash the key again later to retrieve the stored value. We won’t be able to do this unless our hashing function behaves in a predictable and consistent way.

Fortunately, Swift provides a hashValue computed property for types that conform to Hashable. Many types in the standard library conform to Hashable, to include Strings, Integers, and Boolean values.

Instructions

1.

Create a private method, index(for:) with an argument label of for and a String parameter called key.

2.

Inside of index(for:), return the positive value of the String’s hashValue. Remove the commented lines inside the testIndex() function and run the code.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?