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

166
Visualizações
Trouble with Difference Function for Array-based Set Data Structure

(I'm using Group for Set as a name)
if groupA = [1, 2] and groupB = [2, 3]

groupC = groupA.difference(groupB); //-> groupC = [1]  
groupC = groupB.difference(groupA); //-> groupC = [3]  

The former works, but the latter doesn't. Instead of groupC containing 3, it doesn't contain anything. I'm pretty sure all of the other functions work fine. I'm new to JS and I'm not sure what I'm doing wrong.


    class Group
    {
        constructor()
        {
            this.arr = new Array();
        }
        add(elem)
        {
            if(this.has(elem) == false)
            {
                this.arr.push(elem);
            }
        }
        delete(elem)
        {
            for(let i=0; i<this.arr.length; i++)
            {
                if(this.arr[i] == elem)
                {
                    this.arr.splice(i);
                }
            }
        }
        has(elemA)
        {
            for(let elemB of this.arr)
            {
                if(elemA == elemB)
                {
                    return true;
                }
            }
            return false;
        }
        difference(grp)
        {
            var newGroup = new Group();
            for(let i=0; i<this.arr.length; i++)
            {
                newGroup.add(this.arr[i]);
            }
            for(let i=0; i<grp.arr.length; i++)
            {
                newGroup.delete(grp.arr[i]);
            }
            return newGroup;
        }
    }
    
    let a = new Group();
    let b = new Group();
    a.add(1);
    a.add(2);
    b.add(2);
    b.add(3);
    let c = b.difference(a);
    console.log(c.has(1)); //expected false, actually false
    console.log(c.has(2)); //expected false, actually false
    console.log(c.has(3)); //expected true, actually false

about 4 years ago · Juan Pablo Isaza
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