No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Aprende todo un fin de semana sin pagar una suscripción 🔥

Aprende todo un fin de semana sin pagar una suscripción 🔥

Regístrate

Comienza en:

5D
12H
34M
8S

Playground: implementa el método getAllKeys

14/29

Aportes 26

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

o inicia sesión.

Mi aporte

 getAllKeys(){
    const array = this.data
    let key = []

    array.forEach((e) => {
      e.forEach((k) => {
        key.push(k[0]);
      })
    });
    return key;
  }

Mi Solucion

getAllKeys(){
    // Tu código aquí 👈
    let items = this.data
      .filter(item => item != undefined)
      .flat()
      .map(item => item[0])
    
    return items;
  }
  getAllKeys(){
    return this.data
      .flat()
      .map(([name]) => name)
  }
  getAllKeys(){
    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++) {

          if (this.data[i][j]) {
            keys.push(this.data[i][j][0])
          }

        }

      }
    }
    return keys;
  }

Sin Saber en la clase coloqué la respuesta a ambos playgrounds

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

Solución

import { HashTable } from "./hashTable.js"

export class Solution extends HashTable {
  constructor(size){
    super()
    this.data = new Array(size)
  }


  getAllKeys() {
    // Tu código aquí 👈
    return this.data.flat().map(key => key[0]);
  }
}

Creo que esta fue la parte más fácil de todas jaja, mi aporte… 😃

getAllKeys() {
// Tu código aquí 👈
let arr = [];
for (let i of this.data) {
if (typeof i === “object”) {
for (let j = 0; j < i.length; j++) {
arr.push(i[j][0]);
}
}
}
return arr;
}

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

Así fue mi solución usado forEach 😃

# Obtiene todas las llaves de array data
    getAllKeys(){
        let keys = []
        this.data.forEach(element => {
            if(element.length > 1){
                element.forEach(element => {
                    keys.push(element[0])
                })
            }else{
                keys.push(element[0][0]);
            }
        });
        return keys;
    }

Les comparto mí solución

  getAllKeys(){
    const allKeys = this.data.map(bucket => {
      if (bucket) {
        const allKeyBucket = bucket.flatMap(item => item[0]);
        return allKeyBucket;
      }
    });
    return allKeys.flat();
  }

Dejo aquí la forma en la que lo solucioné por si le es de utilidad a alguien

    getAllKeys(){
        const table = this.data;
        let arr = [] //24
        for(let i = 0; i< table.length; i++){  // bucle para recorrer todos los valores de la tabla
            if(table[i]){   //si hay valor en este index 
                arr.push(table[i][0][0]) // agrega el valor a la variable arr 
                if(table[i].length>1){ // comprobar si hay mas valores en la misma entrada
                    for(let j = 1; j<table.length;j++){ 
                    arr.push(table[i][1][0])
                    }      
                }
            }
        }
    console.log(arr)
    return arr;
    }   
}

mi respuesta
*
*
*
*
*
*
*

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

        console.log(result)
      }
    }
    return result

mi solucion:
.
.
.
.
.
.
.
.

.
.
.

getAllKeys() {
        let keys = [];
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i]) { // if (this.data[i].length > 0) {
                for (let j = 0; j < this.data[i].length; j++) {
                    let current = this.data[i];
                    console.log('se agrega al array de keys a ' + current[j][0]);
                    keys.push(current[j][0]);
                }
            } else {
                // la fila esta vacia
            }
        }
        return keys;
    }

the best solution ever

getAllKeys(){
    let allkeys = [];
    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++) {
            allkeys.push(this.data[i][j][0]);
          }
        } else {
          allkeys.push(this.data[i][0][0]);
        }
      }
    }
    return allkeys;
  }

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;

Por aprender flat y map
Flat, por el momento lo hice con puro map

<code> 
  getAllKeys(){
    const buckets = this.data.filter(bucket => bucket !== null)
    const keys = buckets.map(bucket => {
      return bucket.map(arr => arr[0])
    })
    return keys.join().split(",")
  }
getAllKeys() {
    const newArray = [];

    this.data.forEach(array => {
      array.forEach(bucket => {
        newArray.push(bucket[0]);
      })
    })

    return newArray;
  }

Acá mi solución

 getAllKeys() {
    const data = this.data;
    const keys = data.flat().map(key => key[0]);
    return keys;
  }
getAllKeys(){
    const allKeys = [];
    const allBuckets = Object.values(this.data);
    for (let index = 0; index < allBuckets.length; index++) {
      for (let indexInterior = 0; indexInterior < allBuckets[index].length; indexInterior++) {
        allKeys.push(allBuckets[index][indexInterior][0]);
      }
    }
    return allKeys;
  }

Me costo, pero llegue a esta solución

export class Solution extends HashTable {
  constructor(size){
    super()
    this.data = new Array(size)
  }


  getAllKeys(){
    // Tu código aquí 👈
    const listWithoutEmpty = this.data.filter((bucket) => (
      bucket && bucket.length > 0
    ));

    const keys = listWithoutEmpty
      .flat()
      .map(([key]) => key);

    return keys;
  }
}

🥄

  getAllKeys() {
    if (this.data.length) {
      const keys = [];
      this.data.forEach(element => {
        element.forEach(subElement => keys.push(subElement[0]))
      })
      return keys;
    } return undefined;
  }

Hola compañeros, anexo mi resultado para los retos

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) {
          const item = currentBucket[i];
          this.data[address].splice(i, 1);
          return item;
        }
      }
    }
    return undefined;
  }

getAll

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

Importante caer en cuenta acá que nuestra hash table está guardada en un ARREGLO (this.data = new Array(size)), así que para resolver este problema, hay que tratar nuestra hash table como array.

  getAllKeys(){
    const keys = [];
    for (let bucket of this.data) {
      if (bucket) {
        for (let keyValue of bucket) {
          keys.push(keyValue[0]);
        }
      }
    }
    return keys;
  }

Mi solución comentada para mayor entendimiento del código.

.

.

.

.

.

.

.

.

.

.

  getAllKeys() {
    //* filtro solo los buckets con datos
    const filtered = this.data.filter(data => data !== undefined)
    //* Creo un array vacio, donde pusheare mis keys
    let result = []
    //* Recorro el array con los buckets filtrados
    filtered.forEach(ele => {
      //* Si el bucket tiene colision se recorre nuevamente y pushea a mi
      //* array de resultado. Si no hay colision se pushea normalmente.
      if (ele.length > 1) {
        ele.forEach(ele => {
          result.push(ele[0])
        })
      } else {
        result.push(ele[0][0])
      }
    })

    return result
  }

Yo lo hice con métodos de un array.
.

.

.

.

.

.

.

.

.

.

.

.

import { HashTable } from "./hashTable.js"

export class Solution extends HashTable {
  constructor(size){
    super()
    this.data = new Array(size)
  }


  getAllKeys(){
    const data = this.data.filter((item) => item.length);
    const items = data.flat();
    const response = items.map(([key]) => key);

    return response;
  }
}

undefined