Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
How to return all items in array that don't have their opposite?

I have for example this array [-3, 1, 2, 3, -1, 4, -2], and I'd like to return 4 because it doesn't have its own opposite. I've been struggling for several hours to understand how to implement the algorithm. This is what I've done so far:

   let numbers = [-3, 1, 2, 3, -1, 4, -2];
   let sortedNumebrs = [];

    //It returns the numbers array sorted
     function sortedArray() {
         sortedNumebrs = numbers.sort((a, b) => a - b);
         return sortedNumebrs;
      }

      // It return an array with all positive numbers
      function positive() {
         let positive = sortedArray().filter((e) => Math.sign(e) === 1);
         return positive;
      }


      // It return an array with all negative numbers
       function negative() {
        let negative = sortedNumebrs.filter((e) => Math.sign(e) === -1);
        negative = negative.sort((a, b) => a + b);
        return negative;
       }

     // It returns the array longer
     function minLength() {
       let minNum = Math.min(positive().length, negative().length);

       if (minNum === positive().length) {
       return positive()
     } else {
       eturn negative();
    }
   }

     // It returns the array shorter
      function maxLength() {
        let maxNum = Math.max(positive().length, negative().length);
        if (maxNum === positive().length) {
        return positive()
        } else {
         return negative();
        }
       }


  // Function that should return  string if each numbers has its 
  // own opposite otherwise 4
      function opposite() {
         let result = (minLength() === maxLength()) ? true : false;
          if (result) {
         return 'Each element has own opposite';
         } else {     
           // some code
         }
       }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can try something like this:

const yourArray =[-3, 1, 2, 3, -1, 4, -2];
const result = [];

for (let el1 of yourArray) {
  let hasOpposite = false;

  for (let el2 of yourArray) {
    if (el1 === -el2) {
      hasOpposite = true;
      break;
    }
  }

  if (!hasOpposite) {
    result.push(el1);
  }
}

console.log(result); // [4]

or using array functions:

const yourArray = [-3, 1, 2, 3, -1, 4, -2];
const itemsWithoutOpposite = yourArray.filter(el1 => !yourArray.includes(-el1));
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's another approach. This one assumes that duplicates need matching too, so if I have [1, -1, 1] that final 1 should have an additional -1 to match, rather than using the -1 that was matched by the initial 1. It will also not match an element with itself, so if you have a 0 you will need another zero to match.

const yourArr = [-3, 1, 2, 3, -1, 4, -2];

const result = [...yourArr];

for (let i = 0; i < result.length; i++) {
  let el = result[i];
  let inverse = result.indexOf(-el, i + 1);
  if (inverse !== -1) {
    result.splice(inverse, 1);
    result.splice(i, 1);
    i--;
  }
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda