diff --git a/src/TwoOutOfThree2032.java b/src/TwoOutOfThree2032.java deleted file mode 100644 index 7f56bae..0000000 --- a/src/TwoOutOfThree2032.java +++ /dev/null @@ -1,55 +0,0 @@ -import java.util.*; - -public class TwoOutOfThree2032 { - public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) { - Set set1 = new HashSet<>(); - for (int num : nums1) { - set1.add(num); - } - Set set2 = new HashSet<>(); - for (int num : nums2) { - set2.add(num); - } - Set set3 = new HashSet<>(); - for (int num : nums3) { - set3.add(num); - } - Map count = new HashMap<>(); - for (int num : set1) { - if (!count.containsKey(num)) { - count.put(num, 1); - } else { - count.put(num, count.get(num) + 1); - } - } - for (int num : set2) { - if (!count.containsKey(num)) { - count.put(num, 1); - } else { - count.put(num, count.get(num) + 1); - } - } - for (int num : set3) { - if (!count.containsKey(num)) { - count.put(num, 1); - } else { - count.put(num, count.get(num) + 1); - } - } - List res = new ArrayList<>(); - for (Map.Entry entry : count.entrySet()) { - if (entry.getValue() >= 2) { - res.add(entry.getKey()); - } - } - return res; - } - - public static void main(String[] args) { - int[] nums1 = new int[]{1, 1, 3, 2}; - int[] nums2 = new int[]{2, 3}; - int[] nums3 = new int[]{3}; - // [2, 3] - System.out.println(new TwoOutOfThree2032().twoOutOfThree(nums1, nums2, nums3)); - } -} diff --git a/src/main/java/com/leetcode/easy/TwoOutOfThree2032.java b/src/main/java/com/leetcode/easy/TwoOutOfThree2032.java new file mode 100644 index 0000000..fc2a434 --- /dev/null +++ b/src/main/java/com/leetcode/easy/TwoOutOfThree2032.java @@ -0,0 +1,72 @@ +// Tags: Array, Hash Set, Hash Table +package com.leetcode.easy; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class TwoOutOfThree2032 { + + /** + * Returns a list of integers that appear in at least two out of three given arrays. + *

+ * Each value in the result must be unique, even if it appears multiple times across arrays. + *

+ * Time Complexity: O(n₁ + n₂ + n₃) + *

    + *
  • n₁, n₂, n₃ = lengths of nums1, nums2, nums3 respectively
  • + *
  • Creating three HashSets: O(n₁ + n₂ + n₃) - each element inserted once
  • + *
  • Counting occurrences: O(u₁ + u₂ + u₃) where uᵢ ≤ nᵢ (unique elements per array)
  • + *
  • Building result list: O(u) where u = total unique values across all arrays
  • + *
  • Total: O(n₁ + n₂ + n₃) since uᵢ ≤ nᵢ
  • + *
+ *

+ * Space Complexity: O(n₁ + n₂ + n₃) + *

    + *
  • Three HashSets store unique elements: O(u₁ + u₂ + u₃) ≤ O(n₁ + n₂ + n₃)
  • + *
  • HashMap stores counts for all unique values: O(u)
  • + *
  • Result list: at most O(u)
  • + *
  • Worst case: all elements across all arrays are unique
  • + *
+ * + * @param nums1 first integer array + * @param nums2 second integer array + * @param nums3 third integer array + * @return list of integers appearing in at least two arrays + */ + public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) { + Set set1 = new HashSet<>(); + for (int num : nums1) { + set1.add(num); + } + Set set2 = new HashSet<>(); + for (int num : nums2) { + set2.add(num); + } + Set set3 = new HashSet<>(); + for (int num : nums3) { + set3.add(num); + } + Map count = new HashMap<>(); + for (int num : set1) { + count.put(num, count.getOrDefault(num, 0) + 1); + } + for (int num : set2) { + count.put(num, count.getOrDefault(num, 0) + 1); + } + for (int num : set3) { + count.put(num, count.getOrDefault(num, 0) + 1); + } + List res = new ArrayList<>(); + for (Map.Entry entry : count.entrySet()) { + if (entry.getValue() >= 2) { + res.add(entry.getKey()); + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/TwoOutOfThree2032Test.java b/src/test/java/com/leetcode/easy/TwoOutOfThree2032Test.java new file mode 100644 index 0000000..df627e2 --- /dev/null +++ b/src/test/java/com/leetcode/easy/TwoOutOfThree2032Test.java @@ -0,0 +1,55 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class TwoOutOfThree2032Test { + private TwoOutOfThree2032 solution; + + @BeforeEach + void setUp() { + solution = new TwoOutOfThree2032(); + } + + @Test + void testTwoOutOfThree_Example1() { + int[] nums1 = {1, 1, 3, 2}; + int[] nums2 = {2, 3}; + int[] nums3 = {3}; + + List result = solution.twoOutOfThree(nums1, nums2, nums3); + + assertEquals(2, result.size()); + assertTrue(result.contains(3)); + assertTrue(result.contains(2)); + } + + @Test + void testTwoOutOfThree_Example2() { + int[] nums1 = {3, 1}; + int[] nums2 = {2, 3}; + int[] nums3 = {1, 2}; + + List result = solution.twoOutOfThree(nums1, nums2, nums3); + + assertEquals(3, result.size()); + assertTrue(result.contains(2)); + assertTrue(result.contains(3)); + assertTrue(result.contains(1)); + } + + @Test + void testTwoOutOfThree_Example3() { + int[] nums1 = {1, 2, 2}; + int[] nums2 = {4, 3, 3}; + int[] nums3 = {5}; + + List result = solution.twoOutOfThree(nums1, nums2, nums3); + + assertTrue(result.isEmpty()); + } +}