Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
alguien me puede decir para que sirve este codigo

alguien me puede decir para que sirve este codigo especialmente esta línea de código, no puedo entender esta línea ctr[arr[i] - 1]++ ;

 function array_element_mode(arr) { var ctr = [], ans = 0; for (var i = 0; i < 10; i++) { ctr.push(0); } for (var i = 0; i < arr.length; i++) { // what is this code for?? ctr[arr[i] - 1]++; if (ctr[arr[i] - 1] > ctr[ans]) { ans = arr[i] - 1; } } return ans + 1; } console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9]))

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Creo que se supone que esta función debe devolver el modo matemático de una matriz.

Acabo de agregar/arreglar algunos nombres de variables a su función. Esta sigue siendo una implementación terrible, pero espero que las ediciones aclaren lo que hace.

 function array_element_mode2(arr) { var center = [], mode = 0; for (let i = 0; i < 10; i++) { center.push(0); } for (let i = 0; i < arr.length; i++) { const priorElementOfArr = arr[i] - 1; center[priorElementOfArr]++; if (center[priorElementOfArr] > center[mode]) { mode = priorElementOfArr; } } return mode + 1; }
about 4 years ago · Juan Pablo Isaza Relatório

0

Cambié el nombre de las variables y dividí ctr[arr[i] - 1]++; en dos líneas. Se supone que esta función encuentra el número que aparece más en una matriz dada de enteros.

Pero no funcionará si dos o más enteros aparecen el mismo número de veces y si la matriz contiene 0.

 /* * Goal: Find the number which appears most in a given array of integers * Solution: In the ctr array store the number apperences in the following way * ctr[0] appearances of "1" in the array * ctr[1] appearances of "2" in the array * ctr[2] appearances of "3" in the array * ... */ function array_element_mode(arr) { var ctr = [], ans = 0; // fill the ctr array with nulls for (var i = 0; i < 10; i++) { ctr.push(0); } for (var i = 0; i < arr.length; i++) { //////////// here the ctr[arr[i] - 1]++; is splitted into 2 lines // for each array member "find" the correct index to increase const convertArrayMemberToIndexForCtr = arr[i] - 1; // increase the correct index by one ctr[convertArrayMemberToIndexForCtr]++; /////////// // check if the increased index if larger then current answer and if so // store it as the new result if (ctr[convertArrayMemberToIndexForCtr] > ctr[ans]) { ans = convertArrayMemberToIndexForCtr; } } // return the result, but not the index we created before (on line 25), but the real number that is in the array (add the +1 we subtracted before) return ans + 1; } console.log('working example'); console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9])); console.log('this wont work, it shows that "3" is the result, ignoring the "2"'); console.log(array_element_mode([3, 3, 3, 2, 2, 2, 5, 9])); console.log('this wont work as index arr[i] - 1 would then be 0-1=-1'); console.log(array_element_mode([0, 1, 1, 0, 0, 4, 5, 9])); console.log('this wont work, all integers are only once in the array'); console.log(array_element_mode([1, 2, 3, 4, 5, 6, 7, 8]));

about 4 years ago · Juan Pablo Isaza Relatório

0

Creo que esta función es para averiguar qué elemento tiene el mayor número en la matriz

ctr[arr[i] - 1]++:Para contar

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda