Course Content
Subtitle Guide – Hướng dẫn thêm phụ đề
0/1
18 – Thank You
0/1
Master the Coding Interview: Data Structures + Algorithms
About Lesson

One of our ZTM students with a great Discord username, mightypickachu, decided to enhance the previous keys() method to also include hash collision prevention. Here is how to do it (or you can try it yourself):

 keys() {
    if (!this.data.length) {
      return undefined
    }
    let result = []
    // loop through all the elements
    for (let i = 0; i < this.data.length; i++) {
        // if it's not an empty memory cell
        if (this.data[i] && this.data[i].length) {
          // but also loop through all the potential collisions
          if (this.data.length > 1) {
            for (let j = 0; j < this.data[i].length; j++) {
              result.push(this.data[i][j][0])
            }
          } else {
            result.push(this.data[i][0])
          } 
        }
    }
    return result; 
  }