Ad
Code
Diff
  • import static java.util.Collections.addAll;
    import java.util.HashSet;
    
    class Solution {
    
      static int similarPairs(String[] words) {
        var seenWords = new HashSet<String>(words.length);
        addAll(seenWords, words);
        return words.length - seenWords.size();
      }
    
    }
    • import org.junit.jupiter.api.Test;
    • import static java.util.Collections.addAll;
    • import java.util.HashSet;
    • import java.util.Set;
    • class Solution {
    • static int similarPairs(String[] words) {
    • int num = 0;
    • Set<String> seenWords = new HashSet<>();
    • for (String word : words) {
    • if (!seenWords.add(word)) {
    • ++num;
    • }
    • }
    • return num;
    • var seenWords = new HashSet<String>(words.length);
    • addAll(seenWords, words);
    • return words.length - seenWords.size();
    • }
    • }
Code
Diff
  • import java.util.HashSet;
    import java.util.List;
    
    interface Solution {
    
      static int similarPairs(String[] words) {
        return words.length - new HashSet<>(List.of(words)).size();
      }
    
    }
    • import static java.util.Arrays.stream;
    • import java.util.HashSet;
    • import java.util.List;
    • interface Solution {
    • static int similarPairs(String[] words) {
    • return words.length - (int) stream(words).distinct().count();
    • return words.length - new HashSet<>(List.of(words)).size();
    • }
    • }
Code
Diff
  • import org.junit.jupiter.api.Test;
    import java.util.HashSet;
    import java.util.Set;
    
    class Solution {
    
      static int similarPairs(String[] words) {
        int num = 0;
        Set<String> seenWords = new HashSet<>();
        for (String word : words) {
          if (!seenWords.add(word)) {
            ++num;
          }
        }
        return num;
      }
    
    }
    • import org.junit.jupiter.api.Test;
    • import java.util.HashSet;
    • import java.util.Set;
    • class Solution {
    • public static int similarPairs(String[] words) {
    • int num=0;
    • Set<String> seenWords = new HashSet<>();
    • for (String word: words){
    • if (seenWords.contains(word)){
    • num++;
    • }
    • seenWords.add(word);
    • static int similarPairs(String[] words) {
    • int num = 0;
    • Set<String> seenWords = new HashSet<>();
    • for (String word : words) {
    • if (!seenWords.add(word)) {
    • ++num;
    • }
    • return num;
    • }
    • return num;
    • }
    • }