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

181
Views
Encontrar el entero solitario - JavaScript

Considere la matriz [1,2,2]

La matriz contiene dos valores únicos: 1, 2

La matriz contiene valores duplicados: 2

El entero solitario es 1

¿Cómo se puede devolver el entero solitario?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Demostración de trabajo:

 // Array with duplicates const arrWithDuplicates = [1, 2, 2]; var result = arrWithDuplicates.sort().filter((x,i,arr) => x !== arr[i+1] && x !== arr[i-1]); console.log(result); // [1]

about 4 years ago · Juan Pablo Isaza Report

0

 const array = [0,1,2,2,1,5,4,3,4,3,2]; let lonely = array.filter((item,index)=> array.indexOf(item) === array.lastIndexOf(item)); console.log(lonely);

about 4 years ago · Juan Pablo Isaza Report

0

Para una matriz en la que solo le importa tomar el primer entero que es solitario, puede verificar si indexOf y lastIndexOf son iguales. Si lo son, entonces es solitario.

 const array = [2, 2, 1, 3, 4, 3, 4]; const findLonely = (arr) => { for (const num of arr) { if (arr.indexOf(num) === arr.lastIndexOf(num)) return num; } return 'No lonely integers.'; }; console.log(findLonely(array));

Si tiene una matriz que tiene múltiples valores solitarios, puede usar este método para encontrar todos los valores solitarios:

 const array = [2, 2, 1, 3, 4, 3, 4, 6, 8, 8, 9]; const findAllLonely = (arr) => { const map = {}; arr.forEach((num) => { // Keep track of the number of time each number appears in the array if (!map[num]) return (map[num] = 1); map[num]++; }); // Filter through and only keep the values that have 1 instance return Object.keys(map).filter((key) => { return map[key] === 1; }); }; console.log(findAllLonely(array)); // expect [1, 6, 9]

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!