DEV Community

Beatriz Maciel
Beatriz Maciel

Posted on • Edited on

1

HackerRank #39 | HashSet | 🇧🇷

Esse exercício pede para resolvermos usando o HashSet. O HashSet é na verdade um tipo de lista que não repete objetos e isso vai facilitar a resolução do nosso problema.

A partir de um input de um integer t, recebemos um nĂşmero t de pares, podendo ser repetidos ou nĂŁo. Vejamos o exemplo:

5
john tom
john mary
john tom
mary anna
mary anna
Enter fullscreen mode Exit fullscreen mode

Temos 5 pares (delimitados por t) e alguns estĂŁo repetidos. Precisamos filtrar esses pares e contabilizar apenas os que nĂŁo se repetem. No exemplo acima isso significa que precisamos ignorar um dos "john tom" e do "mary anna".
O output precisa mostrar a contabilidade de pares Ăşnicos e fica assim:

1
2
2
3
3
Enter fullscreen mode Exit fullscreen mode

Para fazer isso, seguimos o passo a passo:

  • Fazemos um HashSet<String> hashset = new HashSet<>();, de acordo com a sintaxe do HashSet
  • Fazemos um for que itera a quantidade de pares Ăşnicos, ficando assim:
        HashSet<String> hashset = new HashSet<>();

        for (int i = 0; i < t; i++){
            hashset.add(pair_left[i] + " " + pair_right[i]);
            System.out.println(hashset.size());
        }
Enter fullscreen mode Exit fullscreen mode

Dessa forma, cada par que se forma ocupará uma posição no HashSet e ele não aceitará pares repetidos!

======

ReferĂŞncias

HashSet : Oracle

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

đź‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay