Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!