No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Implementando el m茅todo Get

12/29
Recursos

Aportes 189

Preguntas 15

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

De forma grafica como se llega a cada elemento dentro de los buckets, y del currentBucket, a que arreglo de arreglos de clave, valor:

Saludos 馃槂

馃懇鈥嶐煉火煈ㄢ嶐煉 C贸digo de la clase y soluci贸n al reto de Remove y getAllKeys

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }

  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }
    return undefined;
  }

  getAllKeys() {
    const keys = [];
    for (let i = 0; i < this.data.length; i++) {
      if (this.data[i]) {
        for (let j = 0; j < this.data[i].length; j++) {
          keys.push(this.data[i][j][0]);
        }
      }
    }
    return keys;
  }

  remove(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          const deletedValue = this.data[address][i];
          this.data[address].splice(i, 1);
          return deletedValue;
        }
      }
    }
    return undefined;
  }
}

Creo que ha llegado el momento que rehagan TODOS los cursos de Javascript y que los haga Diego, este es su curso m谩s complejo sobre Javascript que ha ense帽ado y sin embargo se entiende todo, hasta hab铆a entendido la l贸gica del matriz de arrays antes de que lo explique con el [ [], [], 鈥 y eso que yo no suelo entender a la primera鈥

 getKeys() {
    return this.data.reduce((instance, value) => {
      const keys = value.map(([key]) => key);
      return instance.concat(keys)
    }, []);
  }

Dejo el c贸digo del delete (Es pr谩cticamente lo mismo que el get, pero en esta ocasi贸n lo elimina con delete), le puse remove para que el linter de JS no me diera problemas ^^

remove(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    const element = currentBucket[i][1];
                    delete currentBucket[i][1];
                    return element;
                }
            }
        }
        return undefined;
    }

me perdi 馃槮 pero seguire!

My solucion para el metodo getKeys y delete

/* eslint-disable no-restricted-syntax */
/* eslint-disable no-plusplus */
class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }

  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }

  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          const value = currentBucket[i];
          currentBucket.splice(i, 1);
          return value;
        }
      }
    }
    return undefined;
  }

  keys() {
    const keys = [];
    this.data.forEach((bucket) => {
      bucket.forEach((keyPairValues) => {
        keys.push(keyPairValues[0]);
      });
    });
    return keys;
  }

  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }

  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) { return currentBucket[i][1]; }
      }
    }
    return undefined;
  }
}

const myHashTable = new HashTable(50);
myHashTable.set('franz', 1997);
myHashTable.set('laura', 1998);
myHashTable.set('mama', 1966);
myHashTable.set('elena', 2000);
myHashTable.get('franz');
myHashTable.keys();
myHashTable.delete('mama');
myHashTable.keys();

mm no veo aun el potencial de este curso, veo muchas cosas repetidas de otros

// RECIBE LA LLAVE QUE BUSCAMOS CON SU VALOR
    get(key){

        // LLAMAMOS LA FUNCION QUE NOS DEVUELVE UN HASH
        const address = this.hashMethod(key)

        // EL ADDRESS EN DONDE SE ENCUENTRA LA INFORMACI脫N
        const currentBucket = this.data[address]

        // SI ES TRUSTY 
        if( currentBucket )
        {
            // DESDE 0 HASTA EL LARGO DE EL CURRENT BUCKET
            for( let i = 0 ; i < currentBucket.length; i++ ) {

                // SI LA POSICION 0 DE EL ARREGLO ES IGUAL EL 
                // KEY QUE NOS PASARON
                if( currentBucket[i][0] === key) 
                {
                    // DEVOLVEMOS LA POSICION 1 QUE APUNTA A SU VALOR
                    return currentBucket[i][1]
                }
            }

        }
        // DEVOLVEMOS UNDEFINED
        return undefined

 }

Hay un detalle con la funci贸n de set, es que si haces dos veces un set de la misma llave entonces s贸lo lo vas a a帽adir al arreglo y cuando trates de hacer un get te devuelve el primer valor y nunca los que se a帽adieron de manera posterior, estuve viendo que en la implementaci贸n de Hash Table que tiene Java lo que hacen es sobreescribir el valor de la llave con el nuevo, m谩s info al respecto ac谩. Para emular este comportamiento cambi茅 un poco el m茅todo, y qued贸 as铆:

      set(key, value) {
        const address = this.hashMethod(key);
        if (!this.data[address]) {
          this.data[address] = [];
        }
        if (this.data[address].find((element) => element[0] === key)) {
          for (let i = 0; i < this.data[address].length; i += 1) {
            if (this.data[address][i][0] === key) {
              this.data[address][i][1] = value;
            }
          }
        } else {
          this.data[address].push([key, value]);
        }
        return this.data;
      }

Tambi茅n les comparto mis notas sobre 茅ste m贸dulo ac谩 馃榿.

<h3>Resuelto el reto 馃榿</h3>

A帽ad铆 un m茅todo findPair a la clase y lo que hace es abstraer la l贸gica de b煤queda del m茅todo get, para poder usarla a la vez en el m茅todo de delete, y no repetir c贸digo.
Y para aplicar la l贸gica de si buscar un value o eliminar, recibe un callback como segundo par谩metro que inyecta el valor del currentBucket y del index actual

class HashTable {
	constructor(size) {
		this.data = new Array(size);
	}

	hashMethod(key) {
		let hash = 0;

		for (let i = 0; i < key.length; i++) {
			hash = (hash + key.charCodeAt(i) * i) % this.data.length;
		}
		return hash;
	}

	set(key, value) {
		const address = this.hashMethod(key);

		if (!this.data[address]) {
			this.data[address] = [];
		}
		this.data[address].push([key, value]);

		return this.data;
	}

	get(key) {
		return this.findPair(key, (currentBucket, index) => currentBucket[index][1]);
	}

	getAllKeys() {
		const allKeys = [];

		this.data.forEach((bucket) => {
			for (let i = 0; i < bucket.length; i++) {
				allKeys.push(bucket[i][0]);
			}
		})

		return allKeys;
	}

	delete(key) {
		return this.findPair(key, (currentBucket, index) => {
			const pair = currentBucket[index];
			currentBucket.splice(index, 1);
			return pair;
		})
	}

	findPair(key, callback) {
		const address = this.hashMethod(key);
		const currentBucket = this.data[address];

		if (currentBucket) {
			for (let i = 0; i < currentBucket.length; i++) {
				if (currentBucket[i][0] === key) {
					return callback(currentBucket, i);
				}
			}
		}

		return undefined;
	}
}

const myHashTable = new HashTable(50);

myHashTable.set('Diego', 1990);

myHashTable.set("Mariana", 1998);

myHashTable.set("Alejandra", 2000);

Esta es mi propuesta al m茅todo para obtener todas las keys de una hastable.

  getKeys() {
    let keys = [];
    for (let i = 0; i < this.data.length; i++) {
      if (this.data[i]) {
        for (let j = 0; j < this.data[i].length; j++) {
          keys.push(this.data[i][j][0]);
        }
      }
    }
    return keys;
  }

Agradeceria su feedback

Les comparto la forma en que lo hice jaja, y suerte 馃槃

class HashTable {
    constructor(size){
        this.data = new Array(size);
    }

    hashMethod(key){
        let hash = 0;
        for (let i = 0; i < key; i++){
            hash = (hash + key.chartCodeAt(i) * i ) % this.data.length;
        }
        return hash;
    }

    set(key, value){
        const address = this.hashMethod(key);
        if(!this.data[address]){
            this.data[address] = [];
        }

        this.data[address].push([key, value]);

        return this.data;
    }

    get(key){
        const currentBucket = this.getCurrentBucket(key);
        if(currentBucket){
            for(let i = 0; i < currentBucket.length; i++){
                if(currentBucket[i][0] === key){
                    return currentBucket[i][1];
                }
            }
        }
        return undefined;
    }

    delete(key){
        const currentBucket = this.getCurrentBucket(key);
        let item;
        if(currentBucket){
            for(let i = 0; i < currentBucket.length; i++){
                if(currentBucket[i][0] === key){
                    item = currentBucket[i];
                    this.data[address] = currentBucket.filter(j => j[0] !== key);
                }
            }
        }
        return item || undefined;

    }

    getKeys(){
        const address = this.hashMethod();
        const currentBucket = this.data[address];
        return currentBucket.map(i => i[0]);
    }

    getValues(){
        const address = this.hashMethod();
        const currentBucket = this.data[address];
        return currentBucket.map(i => i[1]);
    }

    getAll(){
        return this.getCurrentBucket().map(i => ({ key:i[0], value: i[1] }));
    }

    find(value){
        const result = this.getCurrentBucket().filter(i => i[1] === value).map(i => ({
            key:i[0], value: i[1]
        }))
        return result.length > 1 ? result : result[0] || undefined;
    }

    getCurrentBucket(key){
        let address = key ? 
                        this.hashMethod(key) 
                        : this.hashMethod();
        return this.data[address];
    }
}

const myHashTable = new HashTable(50);

Les comparto mi soluci贸n al reto:

delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    for (let i = 0; i < currentBucket.length; i++) {
      if (currentBucket[i][0] === key) {
        const deleted = currentBucket[i][1];
        this.data[address].splice(i, 1);
        return deleted;
      }
    }
    return undefined;
}

getKeys() {
    let keys = [];
    for (let address of this.data) {
      if (!address) {
        continue;
      }
      for (let [key, value] of address) {
        keys.push(key);
      }
    }
    return keys;
}

realmente me toco hacer el codigo para entender y gracias lo entendi melo sin tantas vueltas

 delete(key){
  const address = this.hashMethod(key)
   const currentBucket  = this.data[address];
  if(currentBucket ){
    for(let i = 0;i < currentBucket.length;i++){
      if(currentBucket[i][0] == key){
        delete currentBucket[i][0]
      }
    }
  }
  return currentBucket
 }

Mi solucion al reto.
getAllKeys.

getAllKeys(){
        const keys = [];
        for (let i = 0; i < this.currentBucket.length; i++){
            if(this.currentBucket[i]){
                for(let j = 0; j < this.currentBucket[i].length; j++){
                    keys.push(this.currentBucket[i][j][0]);
                }
            }
        }
        return keys;
    }

Metodo delete.

delete(key){
        const address = this.hashMethod(key);
        if(!this.currentBucket[address]){
            return null;
        }
        for (let i = 0; i < this.currentBucket[address].length; i++){
            if (this.currentBucket[address][i].key === key){
                return this.currentBucket[address].splice(i,1);
            }
        }
        return null;
    }
<delete(key){
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          const item = currentBucket[i];
          console.log(item);
          for (let j = i; j < currentBucket.length; j++){
            currentBucket[j] = currentBucket[j+1];
          }
          if (currentBucket.length>1){
            delete currentBucket[currentBucket.length - 1];
            currentBucket.length--;
          } else{
            delete this.data[address];
          }
          return item;      
        }
      }
    } else{
        console.log(currentBucket);
        return currentBucket;
    }
  }

  getAllKeys(){
    const allKeys = [];
    for (let i = 0; i < this.data.length; i++){
      if (this.data[i]) {
        const currentBucket = this.data[i];
        for (let j = 0; j < currentBucket.length;j++)
          allKeys.push(currentBucket[j][0]);
      }
    }
    return allKeys;
  }> 
// Creamos una clase HashTable
class HashTable {
    // El constructor recibe un par谩metro 'size'
    constructor(size) {
        // Crea una nueva matriz de tama帽o 'size' y la almacena en el atributo 'data'
        this.data = new Array(size);
    }
    
    // El m茅todo '_hash' recibe una clave 'key' como par谩metro
    _hash(key) {
        // Inicializa una variable 'hash' en cero
        let hash = 0;
        // Recorre la clave 'key' letra por letra
        for (let i = 0; i < key.length; i++) {
            // Calcula el valor hash multiplicando el c贸digo ASCII de la letra por su posici贸n en la clave
            hash = (hash + key.charCodeAt(i) * i) % this.data.length;
        }
        // Retorna el valor hash
        return hash;
    }

    // El m茅todo 'set' recibe una clave 'key' y un valor 'value' como par谩metros
    set(key, value) {
        // Calcula la direcci贸n en la matriz donde se almacenar谩 el par clave-valor utilizando el m茅todo '_hash'
        const address = this._hash(key);
        // Si la direcci贸n a煤n no tiene elementos, crea un nuevo array
        if (!this.data[address]) {
            this.data[address] = [];
        }
        // Agrega el par clave-valor al array de la direcci贸n correspondiente
        this.data[address].push([key, value]);
        // Retorna la matriz completa
        return this.data;
    }

    // El m茅todo 'get' recibe una clave 'key' como par谩metro
    get(key) {
        // Calcula la direcci贸n en la matriz donde deber铆a estar la clave buscada
        const address = this._hash(key);
        // Obtiene el array correspondiente a esa direcci贸n
        const currentBucket = this.data[address];
        // Si el array existe, busca la clave y devuelve su valor
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    return currentBucket[i][1];
                }
            }
        }
        // Si no encuentra la clave, retorna 'undefined'
        return undefined;
    }

    // El m茅todo 'delete' recibe una clave 'key' como par谩metro
    delete(key) {
        // Calcula la direcci贸n en la matriz donde deber铆a estar la clave a borrar
        const address = this._hash(key);
        // Obtiene el array correspondiente a esa direcci贸n
        const currentBucket = this.data[address];
        // Si el array existe, busca la clave y la borra del array
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    const deleted = currentBucket[i];
                    currentBucket.splice(i, 1);
                    // Retorna el par clave-valor borrado
                    return deleted;
                }
            }
        }
        // Si no encuentra la clave, retorna 'undefined'
        return undefined;
    }

    // El m茅todo 'getAllKeys' no recibe par谩metros
    getAllKeys() {
        // Crea un array vac铆o para almacenar todas las claves
        const keysArray = [];
        // Recorre toda la matriz
        for (let i = 0; i < this.data.length; i++) {
            // Si la direcci贸n de la matriz tiene elementos, recorre su array y agrega las claves al array 'keysArray'
            if (this.data[i]) {
                // Recorre cada elemento del array de la direcci贸n correspondiente
                for (let j = 0; j < this.data[i].length; j++) {
                    // Agrega la clave del elemento actual al array de claves
                    keysArray.push(this.data[i][j][0]);
                }
            }
        }
        // Retorna el array de claves completo
        return keysArray;
    }
}

En resumen, el c贸digo define una clase HashTable que implementa una tabla hash para almacenar pares clave-valor. La clase incluye m茅todos para agregar, obtener y eliminar pares clave-valor, as铆 como un m茅todo para obtener todas las claves almacenadas en la tabla hash. Se crea una instancia de la clase y se agregan varios pares clave-valor a la tabla hash. Al final, se imprime la tabla hash completa en la consola.

  remove(key){
    const address = this.hashMethod(key);
    let found = false;
    for(let i = 0; i < this.data[address].length; i++){
      if(found){
        this.data[address][i - 1] = this.data[address][i];
      }
      if(this.data[address][i][0] === key){
        delete this.data[address][i];
        found = true;
      }
    }
    if(found){
      this.data[address].pop();
      return true;
    }else{
      return false;
    }
  }
  keys(){
    return this.data.flat().map(value => value[0]);
  }

M茅todo para borrar elementos seg煤n el key (algo importante es que si ingresas varios valores con el mismo key, al llamar este m茅todo, te borra s贸lo el primero que encuentres):

delete(key){
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        let currentIndex, bucketToDelete;
        if(currentBucket){
            for(let i = 0; i < currentBucket.length; i++){
                if(currentBucket[i][0] === key){
                    currentIndex = i;
                    bucketToDelete = currentBucket[i]
                    break;
                }
            }
           
            (currentIndex == 0) ? delete currentBucket[0] : false;
            for(let i = currentIndex; i < currentBucket.length; i++){
                currentBucket[i] = currentBucket[i+1];       
            }
           currentBucket.length--;
           return bucketToDelete;
        }
        return undefined;
    }

M茅todo para traer todos los key:

getAllKeys(){
        const arrayKeys = [];
        const buckets =  [];
        for(let i = 0; i < this.data.length; i++){
            if(this.data[i] != undefined){
                buckets.push(this.data[i]);
            }
        }
        for(let i = 0; i < buckets.length; i++){
            for(let j = 0; j < buckets[i].length; j++){
                arrayKeys.push(buckets[i][j][0])
            }    
        }
        return arrayKeys;
    }

el code

class HashTable {
  constructor (size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i =0; i<key.length; i++) {
      hash = (hash + key.charCodeAt(i)*i) % this.data.length;
    }
    return hash;
  }

  set(key, value) {
    const address = this.hashMethod(key);
    if(!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }

  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address]; // it will return an array when Colisi贸n de hash happends
    console.log(currentBucket);
    if(currentBucket) {
      for(let i =0; i< currentBucket.length; i++) {
        if(currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }

    return undefined;
  }
}

const myHashTable = new HashTable(50);

// BORRADO
delete(key) {
const address = this.hashMethod(key);
const currentBucket = this.data[address];
if (currentBucket) {
let borrado;
for (let i = 0; i < currentBucket.length; i++) {
if (currentBucket[i][0] === key) {
borrado = currentBucket[i][0];
currentBucket[i] = [];
return borrado;
}
}
}
return undefined;
}

// LISTADO TODAS LAS KEYS
listKeys() {
let existKeys = [];
for (let i = 0; i < this.data.length; i++) {
if (this.data[i]) {
let bucketData = this.data[i];
for (let i = 0; i < bucketData.length; i++) {
existKeys.push(bucketData[i][0]);
}
}
}
return existKeys;
}
}

M茅todo delete:

    delete(key) {
      const address = this.hashMethod(key);
      const currentBucket = this.data[address];

      const index = this.data.indexOf(currentBucket);

      for(let i = index; i < this.data.length - 1; i++) {
        if(this.data[i+1]) {
          this.data[i] = this.data[i+1];
        }
      }
      delete this.data[address];
      delete this.data[this.data.length - 1];
      this.data.length--;
      return currentBucket;
    }

Para obtener todas las keys que existen en la hashtable:

    getAllKeys() {
      const allKeys = [];
      for(let i=0; i < this.data.length; i++) {
        if(this.data[i]) {
          allKeys.push(this.data[i][0][0]);
        }
      }
      return allKeys;
    }

Un peque帽o aporte: se entiende que el profe escribi贸 el m茅todo get de esa forma por motivos pedag贸gicos, para que nosotros le entendamos con facilidad, pero hemos escrito un c贸digo horrorosamente anidado.

  • Podemos pasar de esto:

  • A esto:

Incluso puede refactorizarse de una forma mucho m谩s sencilla que la que yo propongo, te invito a responder este comentario con tu refactorizaci贸n de nuestro m茅todo get.

Podr铆amos a帽adir un comentario a cada l铆nea, indicando brevemente qu茅 hace cada l铆nea o un comentario al inicio del m茅todo describiendo su funcionalidad.
Es buena pr谩ctica siempre estar pensando en c贸mo refactorizar a peque帽a escala tu c贸digo, o sea c贸mo hacerlo m谩s legible y entre esas buenas pr谩cticas, evitar la anidaci贸n es una de ellas.

Les comparto my funci贸n para obtener todas las keys. Luego de tantos intentos y repasar el c贸digo la reduje a una sola l铆nea sin que perdiera su legibilidad, creo jjj

Reto completado, m茅todos delete y allKeys (funcionan en caso de colisiones):

.

.

class HashTable {
	constructor(size) {
		this.bucket = new Array(size);
	}

	//Metodo Hash (para obtenerlo)
	hashMethod(key) {
		let hash = 0;
		for (let i = 0; i < key.length; i++) {
			hash = (hash + key.charCodeAt(i) * i) % this.bucket.length;
		}
		return hash;
	}

	//Metodo insert
	insert(key, value) {
		const address = this.hashMethod(key);
		if (!this.bucket[address]) {
			this.bucket[address] = [];
		}
		this.bucket[address].push([key, value]);
		return this.bucket;
	}

	//Metodo insert
	get(key) {
		const address = this.hashMethod(key);
		const currentBucket = this.bucket[address];
		if (currentBucket) {
			if (currentBucket.length > 1) {
				// Inicio de Logica para colisiones:
				for (let i = 0; i < currentBucket.length; i++) {
					if (currentBucket[i][0] == key) {
						return currentBucket[i][1];
					}
				}
			} else if (currentBucket[0][0] == key) {
				return currentBucket[0][1];
			}
		}
	}

	//Metodo delete
	delete(key) {
		const address = this.hashMethod(key);
		const currentBucket = this.bucket[address];
		if (currentBucket) {
			if (currentBucket.length > 1) {
				// Inicio de Logica para colisiones:
				for (let i = 0; i < currentBucket.length; i++) {
					if (currentBucket[i][0] == key) {
						delete currentBucket[i];
					}
				}
			} else if (currentBucket[0][0] == key) {
				delete currentBucket[0];
			}
		}
	}

	//Metodo allKeys
	allKeys() {
		let allkeys = [];
		for (let i = 0; i < this.bucket.length; i++) {
			let actualBucket = this.bucket[i];
			if (actualBucket) {
				if (actualBucket.length > 1) {
					// Inicio de Logica para colisiones:
					for (let j = 0; j < this.bucket[i].length; j++) {
						allkeys.push(actualBucket[j][0]);
					}
				} else {
					allkeys.push(actualBucket[0][0]);
				}
			}
		}
		return allkeys;
	}
}
const test = new HashTable(100);
test.insert("Fabio", 1990);
test.insert("Yulieth", 1996);
test.insert("Alejandro", 2023);
test.insert("Laurita", 1995);
test.insert("Giova", 2001);
test.insert("Fabio Fernando", 1966);
test.allKeys();

Eliminar un key

delete (key){
    const address = this.hashMethod(key)
    const currentBucket = this.data[address]
    const elementToDelete = [] 
    if (currentBucket){
        const newCurrentBucket = []
        for (let i in currentBucket){
            if (currentBucket[i][0] !== key){
                newCurrentBucket.push(currentBucket[i])
            } else {
                elementToDelete.push(currentBucket[i]) 
            }
        }
        this.data[address] = [ ...newCurrentBucket ]
        return elementToDelete
    } 
    return `${key} does not exist`     
}

Listar todos los keys

getAllKeys (){
    const keys = []
    for (let i in this.data){
        if (this.data[i]){
            for (let j in this.data[i]){
                keys.push(this.data[i][j])
            }
        }
    }
    return keys
}

C贸digo completo

class HashTable{
    constructor(size){
        this.data = new Array(size);
    }
    hashMethod(key){
        let hash = 0;
        for(let i = 0; i < key.length;i++){
            hash = (hash + key.charCodeAt(i)*i) % this.data.length;
        }
        return hash;
    }

    set(key, value) {
        const address = this.hashMethod(key)
        if (!this.data[address]){
            this.data[address] = []
        }
        this.data[address].push([key,value])
        return this.data[address]
    }
    
    get(key){
        const address = this.hashMethod(key)
        const currentBucket =  this.data[address]
        if (currentBucket){
            for (let index in currentBucket){
                if (currentBucket[index][0] === key) {
                    return currentBucket[index][1]
                }
            }
        }
        return undefined
    }

    getAllKeys (){
        const keys = []
        for (let i in this.data){
            if (this.data[i]){
                for (let j in this.data[i]){
                    keys.push(this.data[i][j])
                }
            }
        }
        return keys
    }

    delete (key){
        const address = this.hashMethod(key)
        const currentBucket = this.data[address]
        const elementToDelete = [] 
        if (currentBucket){
            const newCurrentBucket = []
            for (let i in currentBucket){
                if (currentBucket[i][0] !== key){
                    newCurrentBucket.push(currentBucket[i])
                } else {
                    elementToDelete.push(currentBucket[i]) 
                }
            }
            this.data[address] = [ ...newCurrentBucket ]
            return elementToDelete
        } 
        return `${key} does not exist`     
    }
}
// Esta es mi solucion. Complejidad N! Xd

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMetodo(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMetodo(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);

    return this.data;
  }
  get(key) {
    const address = this.hashMetodo(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        console.log(currentBucket[i]);
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }
    return null;
  }

  delete(key) {
    const address = this.hashMetodo(key); //! la pocicion harcodeada por la funcion de hash
    // console.log(this.data[address], address)
    const currentBucket = this.data[address];

    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        // console.log(currentBucket[i]);
        if (currentBucket[i][0] === key) {
          const elementeEliminado = currentBucket[i];
          delete currentBucket[i];
          return elementeEliminado;
        }
      }
    }
    return null;
  }

  getAllKey() {
    let currentKey = this.data;
    let keysEncontradas = [];
    if (currentKey.length > 0) {
      for (let i = 0; i < currentKey.length; i++) {
        if (currentKey[i] === undefined) {
          continue;
        }
        let encontradas = currentKey[i][0][0];
        keysEncontradas.push(encontradas);
      }
    }
    return keysEncontradas;
  }
}

hola sugerencias serian utiles

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash % this.data.length;
  }
  /**El m茅todo set() llamar谩 al m茅todo hash()
   *  para obtener el valor del 铆ndice. */
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }
    return undefined;
  }
  delete(key) {
    const address = this.hashMethod(key); // nos genera el indice
    if (this.data[address].length > 1) {
      const newElements = [];
      for (let i = 0; i <= this.data[address].length - 1; i++) {
        if (this.data[address][i][0] !== key) {
          newElements.push(this.data[address][i]);
        }
      }
      this.data[address] = newElements;
    } else {
      this.data[address] = [];
    }
  }
  getAll() {
    this.data.forEach((values, address) => {
      const valEncadenados = values.map(
        ([key, value]) => `[ ${key}: ${value} ]`
      );
      console.log(`${address}: ${valEncadenados}`);
    });
  }
}

const myHashTable = new HashTable(3);
myHashTable.set("mateo", 1998);
myHashTable.set("monica", 1997);
myHashTable.set("ingrid", 2000);
myHashTable.set("alex", 3000);
myHashTable.delete("monica");
myHashTable.getAll();

asi fue como lo implemente y me funciono

//metodo de eliminar una key
    pop(key){
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if(currentBucket){
            for(let i=0; i < currentBucket.length; i++){
                if(currentBucket[i][0] === key){
                    let value = this.get(key);
                    this.data[address].pop([key, value]);
                }
            }
        }
        return this.data[address];
    }

    //recorriendo el hashtable
    foreachArray(){
      for(let i=0; i< myHashTable.data.length; i++){
        if(myHashTable.data[i] != undefined){
            for(let j=0; j< myHashTable.data[i].length; j++){
            console.log(myHashTable.data[i][j]);
            }
        }
      };
    }
class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }

  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key){
    const address = this.hashMethod(key);
    const currecnkBucked =  this.data[address];
    if(currecnkBucked){
        for(let i = 0; i < currecnkBucked.length; i++){
            if(currecnkBucked[i][0] === key){
                return currecnkBucked[i][1]
            }
        }
    }
    return undefined
  }

  delete(key){
    const address = this.hashMethod(key);
    const currecnkBucked = this.data[address];
    if(currecnkBucked){
        for(let i = 0 ; i <currecnkBucked.length; i++){
            if(currecnkBucked[i][0] === key){
            
                const remove = currecnkBucked[i]
                remove.splice(i,2);
                return remove
            }
        }
    }
    return undefined
  }

  allKey(){
    const key = this.data.map(Element=>
      Element[0][0]
    )
      return key
  }
  
}
const myHashTable = new HashTable(50);

myHashTable.set("pajarito", 1998);
myHashTable.set("pepe", 1990);

Para que no queden espacios vac铆os en los buckets, les dejo una soluci贸n para eliminar:

delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    const deletedItem = currentBucket[i];
                    this.data[address] = currentBucket.filter(item => item[0] !== key);
                    return deletedItem;
                }
            }
        }
        return undefined;
    }

C贸digo de la clase y soluci贸n al reto

class HashTable {
  constructor(size) {
    this.data = new Array(size);
    this.keys = []
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }

  set(key, value) {
    const address = this.hashMethod(key);
    if(!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    this.keys.push(key);
    return this.data;
  }

  get(key) {
    const address =  this.hashMethod(key);
    const currentBucket = this.data[address];

    if(currentBucket[0]) {
      for(let i = 0; i < currentBucket.length; i++) {
        if(currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }     
      }; 
    }
    return undefined;
  }

  deleteElement(key) {
    const address =  this.hashMethod(key);
    const currentBucket = this.data[address];

    if(currentBucket[0]) {
      for(let i = 0; i < currentBucket.length; i++) {
        if(currentBucket[i][0] === key) {
          const deletedElement = currentBucket.splice(i, i + 1);

          if(currentBucket.length < 1) {
            delete this.data[address];
          } 
          this.deleteKey(key);

          return deletedElement;
        }    
      }; 
    }
    return undefined;
  }

  getKeys() {
    return this.keys;
  }
  deleteKey(key) {
    const index = this.keys.indexOf(key);
    this.keys.splice(index, index + 1);
  }
}

const myHashTable = new HashTable(50);

Soluci贸n al reto:

  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] == key) {
          const item = currentBucket[i];
          currentBucket.splice(i, 1);
          return item;
        }
      }
    }
    return undefined;
  }

Hola gente.

Dejo por aqu铆 los c贸digos del reto = ).

delete(key){
        const address = this.hashMethod(key);
        
        let currentBukect = this.data[address];
        for(let i = 0; i < currentBukect.length; i++){
            if(currentBukect[i][0]===key){
                
                delete currentBukect[i];
                return;
            } 
        }     
    }
    getAllkeys(){
        const array = [];
        for(let i = 0; i < this.data.length; i++){
            if(this.data[i]){   
                for(let index = 0; index < this.data[i].length; index++){
                    array.push(this.data[i][index][0]);
                }
            }
        }
        return array;
    }
}

Les dejo mi c贸digo es mi idea solo me base en el c贸digo de un estudiante para agregarlos keys a un nuevo array

class HashTable {
    constructor(size) {
        this.data = new Array(size);
    }
    hashMethod(key) { 
        let hash = 0;
        for(let i = 0; i < key.length; i++) {
            hash = (hash + key.charCodeAt(i) *i) % this.data.length;
        }
        return hash;
    }
    set(key, value) {
        const address = this.hashMethod(key);
        if(!this.data[address]) {
            this.data[address] = [];
        }
        this.data[address].push([key, value]);
        return this.data;
    }
    get(key){
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket) {
        for(let i = 0; i < currentBucket.length; i++) {
            if(currentBucket[i][0] === key) {
                return currentBucket[i][1];
            }
        }
    }
    return undefined;
    }
    delete(key) {
        const address = this.hashMethod(key);
        delete this.data[address][0];
        delete this.data[address][1];
    }
    listKey(){
        const keys = [];
        for(let i = 0; i <= this.data.length; i++) {
            if(this.data[i]) {
                keys.push(this.data[i][0][0]);
            }
            if(this.data[i] && this.data[i][1]) {
                keys.push(this.data[i][1][0]);
            }
        }  
        return keys;    
    }
    
}
const myHashTable = new HashTable(50);

myHashTable.set('Kenia', 1992);
myHashTable.set('Pedro', 1990);
myHashTable.set('Maria', 1991);
myHashTable.set('Jose', 1993);  
myHashTable.set('Carla', 1994);
myHashTable.set('Elizabeth', 1987);
myHashTable.set('Carlos', 1989);
myHashTable.set('Mariana', 1993);
myHashTable.set('Mara', 1993);
myHashTable.set('Mary', 1993);
myHashTable.listKey();

solucion metodo allkeys del hash espero les ayude

keys(){
        //creo el array de almacenamiento
        let key = [];

        //recorro el hash
        for (let i = 0; i < this.data.length; i++){
            //valido si hay contenido
            if(!this.data[i]) continue;

            //si hay contenido recorro la parte interna
            if(this.data[i]){
                
                for(let j=0;j<this.data[i].length;j++){
                    
                    //agrego los keys 
                    key.push(this.data[i][j][0]);
                }
            }
            
        }
        //retorno los keys del hash
        return key;
    }

este metodo permite devolver tanto todos los keys como todos los values del hashtable

 //return the keys of hash
  getIndex(index = Number) {
    return Object.values(this.data)
      .flat()
      .map((item) => item[index]);
  }

soluci贸n del teto para el metodo remove() espero les ayude

//metodo eliminar un elemento
    remove(key){
        //creo el indice a guardar en el array
        const address = this.hasMethod(key);
        
        //asignamos el valor del indice del hash
        const currentBucket = this.data[address]; 
        
        //valido la informacion
        if (currentBucket){

            //busco dentro del array los array iternos 

            for (let i = 0; i < currentBucket.length; i ++){
                //valido que sea igual a la llave
                if (currentBucket[i][0] === key){
                    
                    const deleteItem = this.data[address].splice(i, 1);
                    //metodo splice que corta o elimina desde el indice luego el numero de elementos a eliminar                    
                    //retorno el dato eliminado
                    return deleteItem.flat(); 
                }
            }
        }
    }

Solucion rapida, funciono en unas cuantas pruebas 馃憤 usenlo bajo su propio riesgo. Si no le entienden yo tampoco 馃槄

delete(key) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      return undefined;
    }

    const currentBucket = this.data[address].find((item) => item[0] === key);

    if (this.data[address].length === 1) {
      delete this.data[address];
      return currentBucket;
    }

    const index = this.data[address].findIndex((item) => item[0] === key);

    if (index === -1) {
      return undefined;
    }

    this.data[address].shift(index);

    return currentBucket;
  }

  getAllKeys() {
    return this.data.flat().map((item) => item[0]);
  }

//asi me quedo XD

class HashTable {
    constructor() {
        this.data = new Array(50);
    }
    // es lo que hace internamente un hastTable, le asigna aleatoriamente un address a un bucket
    hashMethod(key) {
        let hash = 0;
        for (let i = 0; i < key.length; i++) {
            hash = (hash + (key.charCodeAt(i) * i)) % key.length;
        }
        return hash;
    }
    set(key, value) {
        const address = this.hashMethod(key);
        //sino existe address , se genera un nuevo array.. pero si ya existe se a帽ade el nuevo arreglo a la address ya existente.
        if (!this.data[address]) {
            this.data[address] = [];
        }
        this.data[address].push([key, value]);
        return this.data;
    }
    get(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    return currentBucket[i][1];
                }
            }
        }
        return undefined;
    }
    delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    const item = currentBucket[i];
                    currentBucket.splice(i,1);
                    return item;
                }
            }
        }
        return undefined;
    }
    keys() {
        let keys = [];
        for (let i = 0; i < this.data.length; i++) {
            const currentBucket = this.data[i];
            if(currentBucket){
                keys.push(...currentBucket.map(bucket  => bucket[0]))
            }
        }
        return keys;
    }
}

const myHashTable = new HashTable();

Mir茅 que con:

Object.getOwnPropertyNames()

Se puede conseguir todo lo que est茅 en los objetos.keys

Buenas, les comparto mi c贸digo.

Documentaci贸n:

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let index = 0; index < key.length; index++) {
      hash = (hash + key.charCodeAt(index) * index) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) this.data[address] = [];
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) return currentBucket.filter((el) => el[0] === key)[0][1];
    return undefined;
  }
  keys() {
    return this.data
      .map((element) => element.map((internalElement) => internalElement[0]))
      .flat();
  }
  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    let deleted = undefined;
    if (currentBucket)
      currentBucket.forEach((el, idx) => {
        if (el[0] === key) {
          deleted = this.data[address][idx];
          this.data[address].splice(idx, 1);
        }
      });
    return deleted;
  }
}

const myHashTable = new HashTable(50);

Hola buenas compa帽eros les dejo mi aporte. Espero que pueda darme feedback 馃槃 saludos!

delete(key){

        const address = this.hashMethod(key);
        const currentBucket = this.data[address];

        if(currentBucket){

            for(let i = 0; i < currentBucket.length; i++){

                if(currentBucket[i][0] === key){

                    delete this.data[address];
                    return true;
                }
            }
        }
        return false;
    }

    getAllKeys(){

        let dataAll = [];
        for(let i = 0; i < this.data.length; i++){
            if(this.data[i]){
                dataAll.push(this.data[i][0][0]);
            }
        }

        return dataAll.flat(1);
    }

codigo del delete y getAllKeys

getAllKeys() {
    const keys = [];
    this.data.forEach((value) => {
      keys.push(value[0][0]);
    });
    return keys;
  }

  delete(key) {
    const address = this.hashMethod(key);
    const correntBucket = this.data[address];
    if (correntBucket) {
      for (let i = 0; i < correntBucket.length; i += 1) {
        if (correntBucket[i][0] === key) {
          const deleteitem = correntBucket[i][1];
          delete correntBucket[i];
          return deleteitem;
        }
      }
    }
    return undefined;
  }```

Mi c贸digo con opciones de formateo

/**
 * Utils
 */

const isTruthy = (element) => element;
const getKey = ([key]) => key;
const mapGetKey = (array) => array.map(getKey);
const label = (title, callback) => {
  console.log("");
  console.log("---------------------------------------");

  console.log("==================================");
  console.log(`======== ${title} ==========`);
  console.log("==================================");

  callback();

  console.log("---------------------------------------");
  console.log("");
};

const hashTableMethodDecorator = (instance, methodDecorated) => {
  const decorator = (...args) => {
    console.log("------------");
    console.log("length before:", instance.data.length);

    const result = instance[methodDecorated](...args);
    console.log(
      `When ${methodDecorated} action was taken, the result was ${result}`
    );

    console.log("length after:", instance.data.length);
    console.log("------------");

    return result;
  };

  return decorator;
};

/**
 * Hash Table Class
 */

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  _hashMethod(key) {
    // Random number 0 - 65.535 Unicode
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this._hashMethod(key);

    if (!this.data[address]) this.data[address] = [];
    this.data[address].push([key, value]);

    return this.data;
  }

  _getBucket(key) {
    const address = this._hashMethod(key);
    const currentBucket = this.data[address];

    return currentBucket;
  }

  _matchKey(key, currentBucket) {
    for (let i = 0; i < currentBucket.length; i++) {
      const [currentKey] = currentBucket[i];
      if (currentKey === key) return currentBucket[i];
    }
  }

  get(key) {
    const currentBucket = this._getBucket(key);

    if (!currentBucket) return undefined;

    const [_, currentValue] = this._matchKey(key, currentBucket);
    return currentValue;
  }

  delete(key) {
    const address = this._hashMethod(key);
    this.data[address] = undefined;
    return undefined;
  }

  getKeys() {
    return this.data.filter(isTruthy).flatMap(mapGetKey);
  }

  getFormatted(key) {
    const found = this.get(key);
    console.log(`Value received from ${key}: ${exampleHashTable.get(key)}`);
    return found;
  }
}

/**
 * Instance hash table
 */

const exampleHashTable = new HashTable(50);

exampleHashTable.set("daniel", 2000);
exampleHashTable.set("diego", 1990);
exampleHashTable.set("mariana", 1980);
exampleHashTable.set("jonathan", 1999);

/**
 * Delete element
 */

label("Delete element", () => {
  exampleHashTable.getFormatted("daniel");
  hashTableMethodDecorator(exampleHashTable, "delete")("daniel");
  exampleHashTable.getFormatted("daniel");
});

/**
 * Get Keys
 */

label("Get Keys", () => {
  console.log(exampleHashTable.getKeys());
});

Buenas tardes,

comparto reto:

class HashTable {
    constructor(size) {
        this.data = new Array(size);
    }

    // hash funtions (metodo que muy rara vez se tiene que crear)
    hashMethod(key) {
        let hash = 0;
        for (let i = 0; i < key.length; i++) {
            hash = (hash + key.charCodeAt(i) * i) % this.data.length; // generacion de numero random
        }

        return hash;
    }

    // metodo que agrega elementos
    set(key, value) {
        const address = this.hashMethod(key);
        // si no existe un valor crea uno nuevo
        if (!this.data[address]) {
            this.data[address] = [];
        }

        // si ya existe crea una colision con un nuevo arreglo
        this.data[address].push([key, value]);
        return this.data;
    }

    // acceso al elemento
    get(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address]; // indice en donde se encuentra el elemento
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) { // se valida si la key existe
                    return currentBucket[i][1];
                }
            }
        }

        // en caso de no encontarr el bucket retorna undefined
        return undefined;
    }

    // eliminando elemento
    delete(key) {
        const address = this.hashMethod(key);
        if (this.data[address]) {
            delete this.data[address];
            return this.data;
        }

        return undefined;
    }

    // retorno de las key
    returnKey() {
        let keys = [];
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i]) {
                for (let j = 0; j < this.data[i].length; j++) {
                    keys.push(this.data[i][j][0]);
                }
            }
        }
        return keys;
    }

}

Gracias.

Una opcion de la funcion get() para los que les gusta simplificar codigo.

get(key){
    const address = this.hashMethod(key);
    return this.data[address]?.find((v)=>v[0] == key)[1];
}

No entendi que tipo de output debemos sacar en getAll 馃

delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];

    if (currentBucket.length > 1) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          this.data[address][i] = null;
        }
      }
    } else {
      this.data[address] = null;
    }

    return undefined;
  }

  getAll() {
    const values = this.data.map((hashArray) => hashArray.map((items) => [items[0], items[1]]));

    return values;
  }

Mi m茅todo para eliminar en el Hash Table:

  delete (key) {
        const address = this.hashMethod(key);
        if ( !this.data[address] ) return undefined;
        const currentBucket = this.data[address];
    
        const deletedItem = currentBucket.find( item => item[0] === key );
        const indexDeletedItem = currentBucket.indexOf(deletedItem);
        currentBucket.splice(indexDeletedItem,1);
        
        return deletedItem;

    }

Mi aporte a continuaci贸n

delete(key){
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if( currentBucket){
            for(let i = 0 ; i < currentBucket.length ; i++){
                if(currentBucket[i][0] === key){
                    delete this.data[address];
                    return currentBucket[i];
                }
            }
        }
        return undefined `The key: ${key}, doesn't exist!`;
    }
returnAllKeys(){
        const keys = [];
        for (let i = 0 ; i < this.data.length ;  i++){
            if(this.data[i]){
                for( let j = 0 ; j < this.data[i].length; j++){
                    keys.push(this.data[i][j][0]);
                }
            }
        }
        return keys;```

Dejo un ejemplo implementado en TypeScript con todos los m茅todos

En mi caso en el set compruebo si existe el elemento y en caso de que exista sobrescribo el valor, puesto que como lo hace el profesor si el key se repite tendr谩s varios con el mismo key lo cual no sirve de nada porque luego no podr谩s acceder a ellos. Haci茅ndolo de esta manera conseguimos un m茅todo con Idempotencia.

const util = require('util');

export class HashTable<K extends string, V> {
  private _length = 0;
  private readonly data: Array<[K, V][]>;
  private readonly size: number;

  constructor(size: number) {
    this.size = size;
    this.data = new Array(size);
  }

  private hash(key: string): number {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash += key.charCodeAt(i);
    }
    return hash % this.size;
  }

  set(key: K, value: V): [K, V][][] {
    const address = this.hash(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    const bucket = this.data[address];
    const element = bucket.find((element) => element[0] === key);
    if (!element) {
      bucket.push([key, value]);
      this._length++;
    } else {
      element[1] = value;
    }
    return this.data;
  }

  get(key: K): V | undefined {
    const address = this.hash(key);
    if (!this.data[address]) return undefined;
    const bucket = this.data[address];
    const element = bucket.find((element) => element[0] === key);
    return (element) ? element[1] : undefined;
  }

  getAll(): [K, V][] {
    const elements: [K, V][] = [];
    this.data.forEach((bucket) => {
      if (bucket) {
        bucket.forEach((element) => {
          elements.push(element);
        });
      }
    });
    return elements;
  }

  getAllKeys(): K[] {
    const keys: K[] = [];
    this.data.forEach((bucket) => {
      if (bucket) {
        bucket.forEach((element) => {
          keys.push(element[0]);
        });
      }
    });
    return keys;
  }

  delete(key: K): V | undefined {
    const address = this.hash(key);
    if (!this.data[address]) return undefined;
    const bucket = this.data[address];
    const element = bucket.find((element) => element[0] === key);
    if (!element) return undefined;
    bucket.splice(bucket.indexOf(element), 1);
    this._length--;
    return element[1];
  }

  get length(): number {
    return this._length;
  }

  [util.inspect.custom]() {
    return this.data;
  }
}

Si os interesa ver m谩s cosas sobre programaci贸n esta es mi web donde subo algunos tutoriales https://blog.jcoder.es

Para el Delete, hice lo siguiente, quedo atento a sus aporte 馃槂

delete(key){
        const address=this.hashMethod(key);
        const currentBucket=this.data[address];
        if(currentBucket){
            for(let i=0;i<currentBucket.length;i++){
            if(currentBucket[i][0]===key){
                    return `Registro [${currentBucket.splice(i,1)}] Eliminado Exitosamente`               
                }
            }
        }
        return undefined
    }

En typescript :

De esta manera resolv铆 HasTable.delete() y HasTable.getAllKeys()
驴Qu茅 opinan?

delete (key) {
    const address = this.hashMethod(key)
    const bucket = this.data[address]
    if (bucket) {
      for (let i = 0; i < bucket.length; i++) {
        if (bucket[i][0] === key) return delete bucket.splice(i, 1)
      }
    }
    return undefined
  }

  getAllKeys () {
    let keys = this.data.flatMap(bucket => {
      if (!bucket) return []
      return bucket.map(record => record[0])
    })

    return keys
  }

Metodo getKeys:

 getKeys() {
        const keys = [];
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i]) {
                for (let j = 0; j < this.data[i].length; j++) {
                    keys.push(this.data[i][j][0]);
                }
            }
        }
        return keys;
    }

methdo getValues:

 getValues() {
        const values = [];
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i]) {
                for (let j = 0; j < this.data[i].length; j++) {
                    values.push(this.data[i][j][1]);
                }
            }
        }
        return values;
    }

metodo delete:

 delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    currentBucket.splice(i, 1);
                }
            }
        }
        return this.data;
    }

De hecho, otra forma de decirlo es que tenemos un array de tuplas

[ [ key, value ], [ key, value ] ]

Esta fue la forma en que lo hice

delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if(currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if(currentBucket[i][0] === key) {
                    delete this.data[address];
                    return this.data;
                }
            }
        }
        return undefined;
    }

    getAllKeys() {
        return this.data.filter(set => set.length > 0).map(bucket => bucket[0][0]);
    }

Yo lo entend铆 as铆 y as铆 lo hice creo que esta bien

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }
    return undefined;
  }
  delete(key) {
    const address = this.hashMethod(key);
    if (this.data[address]) {
      const deleteValue = this.data[address][0]
      this.data.splice(address, 1)
      return deleteValue
    }
    return undefined
  }
  getAllKeys() {
    for (let i = 0; i < this.data.length; i++) {
      if (this.data[i]) {
        console.log(`Key: ${i} - value: ${this.data[i][0]}`);
      }
    }
  }
}

Les comparto mi soluci贸n al m茅todo remove(key)鈥

	remove(key) {
		const address = this.hashMethod(key);
		const currentBucket = this.data[address];
		let currentElement = undefined;
		let index = 0;
		let found = false;
		if (currentBucket) {
			for (let i = 0; i < currentBucket.length; i++) {
				if (found == false && currentBucket[i][0] === key) {
					currentElement = currentBucket[i];
					index = i;
					found = true;
				}
			}
		}
		if(found) {
			this.data[address].splice(index, 1);
			return currentElement;
		}
		return undefined;
	}

<code> 
```    public getAllBucketKeys(key : string) : string[]{
        let bucket : Hash<ValueType>[] = this.get(key)
        let keys : string[] = []
        bucket.map((member : Hash<ValueType>) : void => {
            keys.push(member.key)
        })
        return keys
    }

soluci贸n al reto de Remove y getAllKeys

 delete(key)
    {
        let keyFound = this.hashMethod(key);
       
        if(this.data[keyFound])
        {
            const lengthValueHash = this.data[keyFound].length;
            let  valueDelete;
            for(let i =0; i< lengthValueHash; i++)
            {
                if(this.data[keyFound][i][0] === key)
                {
                    valueDelete = this.data[keyFound][i];
                    if(this.data[keyFound].length>1)
                    {
                        delete this.data[keyFound][1];
                    }
                    if(this.data[keyFound].length === 1)
                    {
                        delete this.data[keyFound];
                    }

                }
            }
            return valueDelete;
        }
        return undefined;
       
    }

    getKeys()
    {
        let keys = [];
        for (let i = 0 ; i<this.data.length;i++)
        {
            
            if(this.data[i])
            {
             const element = this.data[i];
             
             for(let index = 0;index< element.length;index++)
             {
                
                keys.push(element[index][0]);
             }
             
              
            }
        }
        return keys;
    }

esta es la manera que hice el get , si no da un index que no existe entonces devuelve null sino pregunta si la longitud es mayor a 1, significa que hay colisi贸n entonces busca los keys y guarda el key si lo encuentra sino directamente devuelve en la posici贸n 1 ;

get(key){
        let keyFound = this.hashMethod(key);
        let indexValue = 0;
       
        if(this.data[keyFound])
        {
            const lengthValueHash = this.data[keyFound].length;
            if(lengthValueHash > 1)
            {
                for(let i =0; i< lengthValueHash; i++)
                {
                  if(this.data[keyFound][i][0] === key)
                  {
                    indexValue = i;
                  }
                }
            }
            return this.data[keyFound][indexValue][1];
        }
        return null;
       
       
    }

class HashTable {
constructor(size){
this.data =new Array(size);
}

hashMethod(key) {
    let hash = 0;
    for(let i = 0; i < key.length; i++){
        hash = (hash +key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
}

set(key,value){
    const address =this.hashMethod(key);
    if(!this.data[address]){
        this.data[address] =[];
    }
    this.data[address].push([key, value]);
    return this.data;
}

get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket){
        for(let i = 0; i < currentBucket.length; i++){
            if(currentBucket[i][0] === key){
                return currentBucket[i][1];
            }
        }
    }
    return undefined;

}

delete(key){
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket){
        const value = currentBucket[0];
        currentBucket.splice(currentBucket,1);
        return value;

    }
    return undefined;
    
}

listarKeys(){
     var value = [];
     
    for(let i = 0; i < this.data.length; i++){
        if(this.data[i]){
            value.push(this.data[i][0][0])
        }
       
    }
    return value;

}

}

const myHashTable = new HashTable(50);

Elimina un item especifico segun su key

deleteItem(key){
    const address = this.hashMethod(key);
    if(this.data[address]){
      delete this.data[address];
    }else console.log("Not found");
    return this.data;
  }

Devuelve las entradas con su informacion almacenadas en el bucket

getAllKey(){
      const addresses = this.data.length;
      for(let i = 0; i < addresses; i++){
        if(this.data[i] != null){
          console.log(this.data[i][0]);
        }
      }
  }

Esta es mi implementaci贸n de getAllKeys y remove,

class hashTable {
    constructor(size){
        this.data = new Array(size);
    }

    hashMethod(key){
        let hash = 0;
        for (let i = 0; i < key.length; i++){
            hash = (hash + key.charCodeAt(i)*i) %this.data.length;
        }
        return hash;
    }

    set(key,value){
        const address = this.hashMethod(key);
        if(!this.data[address]){
            this.data[address] = [];
        }
        this.data[address].push([key,value]);
        return this.data
    }

    get(key){
        const address = this.hashMethod(key);
        const arrayIn = this.data[address];
        if(arrayIn != null){
            for (let i = 0; i<arrayIn.length; i++){
                if (key === arrayIn[i][0]){
                    return arrayIn[i];
                }
            }
        }

        return undefined;
    }


    getAllKeys(){
        const allKeys = []
        for (let i = 0;i<this.data.length;i++){
            const arrayIn = this.data[i];
            if (arrayIn){
                for (let j = 0;j<arrayIn.length;j++){
                    allKeys[allKeys.length] = arrayIn[j][0];
                }                
            }
        }
        return allKeys;
    }

    remove(key){
        const address = this.hashMethod(key);
        const arrayIn = this.data[address];
        if(arrayIn){
            if(arrayIn.length == 1){
                delete this.data[address]
            }
            else{   
                for (let i = 0; i<arrayIn.length; i++){
                    if (key === arrayIn[i][0]){
                        for (let j = 0; j<arrayIn.length-1-i;j++){
                            this.data[address][i] = this.data[address][i+1]; 
                        } 
                    }
                }
                this.data[address].pop();

            }
        }
        return undefined;
    }
}

const myNewArray = new hashTable(50); 

Yo llamo arrayIn a lo que en el video nombran como currentBucket porque ya lo hab铆a hecho y me di贸 flojera cambiarlo, saludos!

My code

	delete(key) {
		const address = this.hashMethod(key);
		delete this.data[address];
	}

	getAll() {
		const allKeys = [];
		for (const key in this.data) {
			if (Object.hasOwnProperty.call(this.data, key)) {
				allKeys.push(this.data[key]); 
			}
		}
		return allKeys.flat();
	}

Dejo la soluci贸n del reto

delete(key)
  {
    const address = this.hashMethod(key)
    const currentBucket = this.data[address]
    if(currentBucket)
    {
      if(currentBucket.length > 1)
      {
        for(let i=0; i< currentBucket.length; i++)
        {
          if(currentBucket[i][0] === key)
          {
            this.data[address].splice(i,1)
            return key
          }
        }
      }
      this.data[address].shift()
      return key
    }
    return undefined
  }


getKeys()
  {
    const keys = []
    for(let i=0; i<this.data.length; i++)
    {
      if(this.data[i])
      {
        if(this.data[i].length>1)
        {
          for(let j=0; j<this.data[i].length;j++)
          {
            keys.push(this.data[i][j][0])
          }
        }
        else keys.push(this.data[i][0][0])
      }
    }
    return keys
  }

Asi lo hice yo

delete(key){
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    let deleted;
    if(currentBucket){
        if(currentBucket.length > 1){
            for(let i = 0; i< currentBucket.length; i++){
                if(currentBucket[i][0] === key){
                    deleted = currentBucket[i];
                    for(let idx = i; idx < currentBucket.length; idx++){
                        currentBucket[idx] = currentBucket[idx + 1];
                    }
                    currentBucket.pop();
                    return deleted;
                }
            }
        }
        deleted = currentBucket[0];
        delete currentBucket[0];
        return deleted;
    }
    return undefined;
}
clear(){
    this.data = new Array(this.size);
    return this.data;
}
驴Para que se usan? Las tablas de has son estructuras de datos asociativos y se usan principalmente para almacenar grandes vol煤menes de informaci贸n de manera que puedan ser accedidos de una forma m谩s r谩pida. Dependiendo del algoritmo de hash utilizado son m谩s o menos efectivos.

Comparto el Get para todos las key, vi soluciones complicadas, capaz la mia no es muy practica pero es m谩s sencilla a las que vi:

getKeys(){
        var keys=[]
        for ( let i=0 ; i<=this.data.length ; i++ ) {
            if ( this.data[i] != undefined ) {
            keys.push( this.data[i] );
            }
        }
        return keys;
    }

Aqui esta mi codigo con el metodo delete. El unico detalle que encontre en esta clase es que pasa cuando quieres sobre escribir un valor, habria que crear un metodo update pues con el set no se puede a menos que se modidifique pero veo que serian demasiados pasos.
Tu que piensas?

class HashTable {
	constructor(size) {
		this.data = new Array(size);
	}
	hashFunctioner(key) {
		let hash = 0;
		for (let i = 0; i < key.length; i++) {
			hash = (hash + key.charCodeAt(i) * i) % this.data.length;
		}
		return hash;
	}
	setter(key, value) {
		const address = this.hashFunctioner(key);
		console.log(address);
		if (!this.data[address]) {
			this.data[address] = new Array();
		}
		this.data[address].push([key, value]);
		return this.data;
	}
	getter(key) {
		const address = this.hashFunctioner(key);
		const bucket = this.data[address];
		if (bucket) {
			for (let i = 0; i < bucket.length; i++) {
				if (bucket[i][0] === key) {
					return bucket[i][1];
				}
			}
		}
		return null;
	}
	deleter(key) {
		const address = this.hashFunctioner(key);
		const bucket = this.data[address];
		if (bucket) {
			for (let i = 0; i < bucket.length; i++) {
				if (bucket[i][0] === key) {
					bucket.splice(i, 1);
					return bucket;
				}
			}
		}
		return null;
	}
}
  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          this.data[address].splice(i, 1);
          return true;
        }
      }
    }
    return false;
  }
  getKeys() {
    const keys = [];
    for (let i = 0; i < this.data.length; i++) {
      if (this.data[i]) {
        for (let j = 0; j < this.data[i].length; j++) {
          keys.push(this.data[i][j][0]);
        }
      }
    }
    return keys;
  }

Holaaa, les dejo mi resolucion del reto.

//Meotdos para encontrar, eliminar,etc todos los datos

class HashTable {
    constructor(size){
        this.data = new Array(size);
    }
    //Las hashFucntion no las tenes que hacer, la realiad es que ya existen en github o google, una de ellas es la que tengo aca abajo,peero, lo mejor es googlear la mejor que se adapte a mi estilo.
    hashMethod(key) {
        let hash = 0;
        for(let i = 0; i < key.length; i++) {
            hash = (hash + key.charCodeAt(i) * i) % this.data.length;
        }
        return hash;
    }
    set(key,value){
        const address = this.hashMethod(key);
        if(!this.data[address]) {
            this.data[address] = [];
        } 
        this.data[address].push([key,value]);
        return this.data;
    }
    get(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address]
        if(currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if(currentBucket[i][0] === key){
                    return currentBucket[i][1]
                }
            }
        }
    }
    delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address]
        if(currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if(currentBucket[i][0] === key){
                  console.log(currentBucket)
                  
                 const inndex = currentBucket.indexOf(currentBucket[i])
                  console.log(inndex)
                  const deleted = currentBucket.splice(inndex,1)
                  console.log(currentBucket) 
                    
                }
            }
        }
    }
		getKeys(){
     const keys = []
     const concat = this.data.flat(1)
     console.log(concat)
     for(let i = 0; i < concat.length;i++) {
    		keys.push(concat[i][0])
  
     }
     console.log(keys)
   }

}



//address es el numero que genera la funcion hash con la key.
//key es la palabra que guardaremos
//value es un valor de lo que nos guste guaradar

const myHashTable = new HashTable(50);
myHashTable.set('Diego',2001)
myHashTable.set('Mariana',1342)
myHashTable.get('Mariana')
myHashTable.delete('Mariana')

Impresionante clase, me encanta

No lo se Rick, obtener el valor de un HashTable tiene una complejidad de O(1) y aqu铆 parece O(n)

Aqui el codigo un poco mas peque帽o utilizando la webAPI de find

get(key){
    const address = this.hashMethod(key)
    const currentBucket = this.data[address]

    if(currentBucket){
        const value = currentBucket.find(item => item[0] === key)
        return value[1]
    }
    return undefined;
}

Metodos Borrrar y llavesObjetos!

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }
  hashMethod(key) {
    let hash = 0;
    for (let i = 0; i < key.length; i++) {
      hash = (hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        }
      }
    }
    return undefined;
  }

  borrar(key){
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket){
      for(let i = 0 ; i < currentBucket.length; i++){
        if(currentBucket[i][0] === key){
          delete this.data[address];
          return currentBucket
        }
      }
    }
    return undefined
  }
  llavesObjeto() {
    const keys = [];
    for(let i = 0; i < this.data.length ; i++){
      if(this.data[i]){
        for(let e = 0; e < this.data[i].length ; e++){
          keys.push(this.data[i][e][0])
        }
      }
    }
    return keys;
  }
}

Mi c贸digo:
Me hubiera gustado no haber usado el m茅todo 鈥渟plice鈥, pero no supe c贸mo hacer el 鈥渄elete鈥 y que el tama帽o del array de keys con el mismo hash se redujera. COMENTARIOS POR FAVOR!

delete(key) {
  const address = this.hashMethod(key)
  const currentBucket = this.data[address]
  if (currentBucket) {
    for (let i = 0; i < currentBucket.length; i++) {
      if (currentBucket[i][0] === key) {
        currentBucket.splice(i, 1)
        return key
      }
    }
  }
  return false
}

getKeys() {
  return this.data.reduce((keysArray, values) => {
    const keys = values.map(([key]) => key);
    return keysArray.concat(keys)
  }, []);
}

Creo que el m茅todo 鈥渄elete鈥 no deber铆a devolver el 鈥渧alue鈥 por el principio de responsabilidad 煤nica. Estamos programando un delete, no un get(key).

Delete method:

  remove(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];

    if (currentBucket) {
      if (currentBucket.length > 1) {
        for (let i = 0; currentBucket.length; i++) {
          if (currentBucket[i][0] === key) {
            const value = currentBucket[i];
            currentBucket.splice(i, 1);
            return value;
          }
        }
      }
      if (currentBucket.length === 1) {
        const index = this.data.indexOf(currentBucket);
        this.data.splice(index, 1);
      }
    }
  }

Este man es muy inteligente 馃く

Aqu铆 mi soluci贸n para los desaf铆os (son los m茅todos al fondo de la class, 鈥渄elete()鈥 y 鈥済etKeys()鈥):

class HashTable{
    constructor(size){
        this.data = new Array(size)
    }
    hashMethod(key){
        let hash = 0
        for(let i = 0; i < key.length; i++){
            hash = (hash + key.charCodeAt(i) * i) % this.data.length
        }
        return hash
    }

    set(key, value){
        const address = this.hashMethod(key)
        if(!this.data[address]){
            this.data[address] = []
        }
        this.data[address].push([key, value])
        return this.data
    }

    get(key){
        const address = this.hashMethod(key)
        const currentBuket = this.data[address]
        if(currentBuket){
            for(let i = 0; i < currentBuket.length; i++){
                if(currentBuket[i][0] === key){
                    return currentBuket[i][1]
                }
            }
        }
        return undefined
    }
    delete(key){
        const address = this.hashMethod(key)
        const currentBuket = this.data[address]
        let valueKey
        if(currentBuket){
            for(let i = 0; i < currentBuket.length; i++){
                if(currentBuket[i][0] === key){
                    valueKey = currentBuket[i]
                    currentBuket.splice(i, 1)
                }
            }
        }
        console.log(currentBuket.length, currentBuket)
        return valueKey
    }

    getKeys(){
        const allKeys = []
        for(let i = 0; i < this.data.length; i++){
            const keyType = this.data[i]
            console.log(keyType)
            if(Array.isArray(keyType) && this.data[i].length > 0){
                for(let indx = 0; indx < keyType.length; indx++){
                    allKeys.push(keyType[indx][0])
                }
            }
        }
        return allKeys
    }
}

Listo el reto de la clase, me gusto, me hizo pensar en las estructuras y usar otras cosas que no fueran los cl谩sicos m茅todos que ofrece JavaScript

class hashTable {
  constructor() {
    this.data = Array(50);
  }
  hashMethod(key) {
    let hash = 0; 
    for (let i = 0; i < key.length; i++) {
      hash = ( hash + key.charCodeAt(i) * i) % this.data.length;
    }
    return hash;
  }
  set(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = []
    }
    this.data[address].push([key, value]);
    return this.data;
  }
  get(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          return currentBucket[i][1];
        } 
      }
    }
    return undefined;
  }
  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if(currentBucket) {
      for (let i = 0; i < currentBucket.length; i++) {
        if (currentBucket[i][0] === key) {
          const item = `[${currentBucket[i][0]}: ${currentBucket[i][1]}] Deleted`
          delete currentBucket[i][0];
          delete currentBucket[i][1];
          return item;
        } 
      }
    }
    return "Not exist";
  }
  getAll() {
    const finded = []
    for (let i = 0; i < 50; i++) {
      const currentBucket = this.data[i]
      if(currentBucket) {
        for (let i = 0; i < currentBucket.length; i++) {
          if (currentBucket[i][0]){
            finded.push([currentBucket[i][0], currentBucket[i][1]])
          }
        }
      }
      
    }
    return finded
  }
}

const myHashTable = new hashTable();

myHashTable.set("Juan", 1990);
myHashTable.set("Elizabeth", 1996);
myHashTable.set("Diego", 1994);
myHashTable.set("Jose", 2000);
myHashTable.set("Juan", 1985);
myHashTable.set("Mariana", 1998);

myHashTable.get("Diego");

myHashTable.delete("Diego");

myHashTable.getAll();

Hola 馃榾.
Les comparto mi soluci贸n de los dos retos que nos han dejado. Gracias por su atenci贸n !

// Delete an element 

  deleteElement( key )
  {
    const address = this.hashMethod(key)
    const currentBucket = this.data[address]

    if(currentBucket)
    {
      for ( let i = 0 ; i < currentBucket.length ; i++ )
      {
        if( currentBucket[i][0] === key )
        {
          delete currentBucket[i]
        }
      } 
    }
    return undefined
  }

  // Return all the keys
  getAllKeys()
  {
    let listOfKeys = [  ]
    for ( let i =  0 ; i < this.data.length - 1 ; i++ )
    {
      if( this.data[i] !==  undefined )
      {
        for( let l = 0 ; l < this.data[i].length ; l++ )
        {
          listOfKeys.push( this.data[i][l][0] )
        }
      }
    }
    return listOfKeys
  }

Esta es una propuesta de la clase HashTable, junto con los m茅todos getAllKeys y delete 馃憞

class HashTable {

    constructor(size) {
        this.data = new Array(size);
    }

    hashMethod(key) {
        let hash = 0;
        for (let i = 0; i < key.length; i++) {
            hash = (hash + key.charCodeAt(i) * i) % this.data.length;
        }
        return hash;
    }

    set (key, value) {
        const address = this.#getAddress(key);
        if (!this.data[address]) {
            this.data[address] = [];
        }
        this.data[address].push([key, value]);
    }

    get (key) {
        const currentBucket = this.#getCurrentBucket(key);
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] == key) {
                    return currentBucket[i][1];
                }
            }
        }
        return undefined;
    }

    delete (key) {
        const currentBucket = this.#getCurrentBucket(key);
        if (currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    const item = currentBucket[i];
                    //delete currentBucket[i];
                    currentBucket.splice(i,1);
                    return item;
                }
            }
        }
        return undefined;
    }

    getAllKeys() {
        const keys = [];
        let key;
        let bucket;
        for(let i = 0; i < this.data.length; i++) {
            bucket = this.data[i];
            if (bucket) {
                for(let j = 0; j < bucket.length; j++) {
                    key = bucket[j];
                    if (key) {
                        console.log(key);
                        keys.push(key[0])
                    }
                }
            }
        }
        return keys;
    }

    #getAddress (key) {
        return this.hashMethod(key);
    }

    #getCurrentBucket (key) {
        const address = this.#getAddress(key);
        const currentBucket = this.data[address];
        return currentBucket;
    }
}

const myHashTable = new HashTable(50);

// TEST
myHashTable.set("Sandro", 1967);

console.log(myHashTable.data);

myHashTable.set("Mariana", 1997);
myHashTable.set("Diego", 2000);

console.log(myHashTable.data);

console.log(myHashTable.get("Sandro"));
console.log(myHashTable.get("Mariana"));
console.log(myHashTable.get("Diego"));
console.log(myHashTable.get("Julian"));

console.log(myHashTable.delete("Diego"));
console.log(myHashTable.data);

console.log(myHashTable.getAllKeys());
/*Es el mismo aporte de la clase pasada, con el plus que nuestro intento de comprobacion dedujo el metodo get de esta clase*/
class HashTable {
    constructor(size) {
        this.data = new Array(size)
    }
    hashMethod(key) {
        let hash = 0;
        for (let i = 0; i < key.length; i++) {
            hash = (hash + key.charCodeAt(i) * i) % this.data.length;
        }
        return hash
    }
    set(key, value) {
        const address = this.hashMethod(key)
        if (!this.data[address]) {
            this.data[address] = []
        }
        this.data[address].push([key, value])
        return this.data
    }
    //IMPLEMENTACION DEL GET DEDUCIDO DE FORMA SIMPLE
    // get(key) {
    //     const address = this.hashMethod(key)
    //     return this.data[address]
    // }
    //LA FORMA EXACTA PARA IDENTIFICAR EL VALOR PRECISO
    get(key) {
        const address = this.hashMethod(key)
        const currentBucket = this.data[address]
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    return currentBucket[i][1]
                }
            }
        } else {
            return undefined
        }
    }
}

//Generamos 50 buckets
const myHashTable = new HashTable(50)
// console.log(myHashTable)
myHashTable.set("Diego", 1990)
myHashTable.set("Mariana", 1998)
myHashTable.set("Alejandra", 2000)
// const address = myHashTable.hashMethod("Diego")
// console.log(myHashTable.data[address])
// console.log(myHashTable.data[10])
/**La funcion get se dedujo al tratar de comprobar como mi comentario de la clase pasada */
console.log(myHashTable.get('Diego'))
// console.log(myHashTable)



getAllKeys(){
        return this.data.reduce((keys, bucket) => {
            const bucketKeys = bucket.map(key => key[0]);
            return keys.concat(bucketKeys);
        }, []);
    }

Los m茅todos

delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if (currentBucket) {
            for (let i = 0; i < currentBucket.length; i++) {
                if (currentBucket[i][0] === key) {
                    currentBucket.splice(i, 1);
                    if (currentBucket.length === 0) {
                        delete this.data[currentBucket];
                    }
                    return this.data;
                }
            }
        }
        return undefined;
    }

    getAllKeys(){
        const keys = [];
        this.data.forEach(bucket =>{
            bucket.forEach(array => {
                keys.push(array[0])
            })
        })
        return keys;
    }

Les comparto el m茅todo para solamente obtener las keys, de nuestra hashTable, observen que en realidad tenemos un array, de arrays, de arrays, es decir 3 arrays anidados, a mi m me vol贸 la cabeza porque apenas entend铆 eso, cuando tenemos un adress, tenemos tres arrays anidados, por que no estamos accediendo a ning煤n espacio espec铆fico, estamos desde el scope m谩s general con el que podemos acceder a nuestra hashTable

  getKeys(){
        let keys = []
        console.log(this.data)
        for(let i=0; i<this.data.length; i++){
            if (this.data[i]){
                keys.push(this.data[i][0][0])
            }
        }

        return keys ? keys : undefined
    }

Comporto mi codigo de la clase y el reto

class HashTable {
    constructor(size){
        this.data = new Array(size);
    }
    hashMethod(key){
        let hash = 0;
        for (let i = 0; i < key.length; i++){
            hash = (hash + key.charCodeAt(i) * i) % this.data.length;
        }
        return hash;
    }
    set(key, value){
        const address = this.hashMethod(key);
        if(!this.data[address]){
            this.data[address] = [];
        }
        this.data[address].push([key, value]);
        return this.data;
    }
    keys() {
        const buckets = Object.values(this.data);
        let keys = [];
        buckets.forEach(bucket => {
            bucket.forEach(array => keys.push(array[0]));
        });
        return keys;
    }
    get(key){
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if(currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if(currentBucket[i][0] === key) {
                    console.log(currentBucket);
                    return currentBucket[i][1];
                }
            }
        }
        return undefined;
    }
    delete(key) {
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if(currentBucket) {
            for(let i = 0; i < currentBucket.length; i++) {
                if(currentBucket[i][0] === key) {
                    let arrayDelete = currentBucket[i];
                    currentBucket.splice(i, 1);
                    return arrayDelete;
                }
            }
        }
        return undefined;
    }
}

const myHashTable = new HashTable(50);

Les dejo mi m茅todo delete:

Reto 1 de la clase

deleted(key){
        const address = this.hashMethod(key);
        const currentBucket = this.data[address];
        if(currentBucket){   
            delete this.data[address];
            return this.data[address];
        }else{
            alert('no existe ese key');
        }
    }

Reto 2 de la clase

 getAllValues(){
        for (let i = 0; i < this.data.length; i++) {
            const bucket = this.data[i];
            if(bucket !== undefined){
                console.log(`key: ${bucket[0][0]} value: ${bucket[0][1]}` );
            }
        }
    }

reto satisfactorio, Ok!, en la imagen en apartado verde tengo una colision de hash table donde, con el metodo delete se borra el key/value del array鈥橻0], entonces la informacion contenedora del bucket random, al momento de aplicar el metodo delete, me retorno un array vacio, como el bucket 49, la idea es que se borrara todo el array como lo que sucede en la collision cierto?.. ahi tengo dudas campeon. Pero bueno esto es un reto鈥 aun hace falta.

 remove(key) {
    const address = this.hashMethode(key);
    if(this.data[address] && this.data[address].length) {
      for (let i = 0; i < this.data.length; i++) {
        if(this.data[address][i][0] === key) {
          this.data[address].splice(i, 1);

          return `${key} was erase`;
        }
      }
    } else {
      return `The key ${key} does not exist`;
    }
  }
  getAllKeys() {
    return this.data.reduce((acc, val) => {
      if(val) {
        acc.push(...val.map(element => element[0]));
        return acc;
      }
    }, [])
  }

Listos los retos 馃槃

unset(key){
    const address = this.hashMethod(key)
    const currentBucket = this.data[address]
    if(currentBucket){
        for(let i = 0; i < currentBucket.length; i++){
            if(currentBucket[i][0] === key ){
                const eliminateValue = currentBucket[i]
                this.data[address].splice(i,1)
                return eliminateValue
            }
        }
    }
}   

allKeys(){
    const listOfKeys = []
    this.data.forEach(element => {
        element.forEach(element =>{
            listOfKeys.push(element[0])
        })
    })
    return listOfKeys
}


Mi soluci贸n al reto 馃榾:

delete(key) {
  const address = this.hashMethod(key);
  const currentBucket = this.data[address];
  if (currentBucket) {
    for (let i = 0; i < currentBucket.length; i++) {
      if (currentBucket[i][0] === key) {
        let deletedItem = currentBucket[i];
        currentBucket.splice(i, 1);
        return deletedItem;
      }
    }
  }
  return undefined;
}

getAllKeys() {
  let keys = [];
  for (let i = 0; i < this.data.length; i++) {
    if (!this.data[i]) continue;
    for (let j = 0; j < this.data[i].length; j++) {
      keys.push(this.data[i][j][0]);
    }
  }
  return keys;
}

Comparto mi soluci贸n del reto

class HashTable {
  constructor(size) {
    this.data = new Array(size);
  }

  hashMethod(key) {
    let hash = 0;
    for (let chart of key) {
      hash += chart.charCodeAt(0);
    }
    hash = hash % this.data.length;
    return hash;
  }

  insert(key, value) {
    const address = this.hashMethod(key);
    if (!this.data[address]) {
      this.data[address] = [];
    }
    if (this.data[address].find((bucket) => bucket.key === key)) {
      return console.log("The key was assigned before");
    }
    this.data[address].push({ key, value });
    return this.data;
  }

  search(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    return currentBucket
      ? currentBucket.find((element) => element.key === key).value
      : undefined;
  }
  delete(key) {
    const address = this.hashMethod(key);
    const currentBucket = this.data[address];
    if (currentBucket) {
      const index = currentBucket.findIndex((element) => element.key === key);
      if (index !== -1) {
        this.data[address].length === 1
          ? delete this.data[address]
          : this.data[address].splice(index, 1);
      }
      return this.data;
    }
  }
  keys() {
    return this.data.flat().map((element) => element.key);
  }
}