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();
- }
- }
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();
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void testSomething() { assertEquals(0, Solution.similarPairs(new String[] { "Volvo", "BMW", "Ford", "Mazda" })); assertEquals(2, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "Mazda" })); assertEquals(3, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "BMW", "Mazda", "BMW" })); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
@Testvoid testSomething() {String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};String[] cars2 = {"Volvo", "Volvo", "Volvo", "Mazda"};assertEquals(Solution.similarPairs(cars), 0);assertEquals(Solution.similarPairs(cars2), 2);}- @Test
- void testSomething() {
- assertEquals(0, Solution.similarPairs(new String[] { "Volvo", "BMW", "Ford", "Mazda" }));
- assertEquals(2, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "Mazda" }));
- assertEquals(3, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "BMW", "Mazda", "BMW" }));
- }
- }
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;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void testSomething() { assertEquals(0, Solution.similarPairs(new String[] { "Volvo", "BMW", "Ford", "Mazda" })); assertEquals(2, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "Mazda" })); assertEquals(3, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "BMW", "Mazda", "BMW" })); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
@Testvoid testSomething() {Solution aaa = new Solution();String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};assertEquals("actual", "actual");assertEquals(aaa.similarPairs(cars), 0);}- @Test
- void testSomething() {
- assertEquals(0, Solution.similarPairs(new String[] { "Volvo", "BMW", "Ford", "Mazda" }));
- assertEquals(2, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "Mazda" }));
- assertEquals(3, Solution.similarPairs(new String[] { "Volvo", "Volvo", "Volvo", "BMW", "Mazda", "BMW" }));
- }
- }