| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.TwoOutOfThree; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class TwoOutOfThreeHashSet : ITwoOutOfThree |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums1"></param> |
| | 22 | | /// <param name="nums2"></param> |
| | 23 | | /// <param name="nums3"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public IList<int> TwoOutOfThree(int[] nums1, int[] nums2, int[] nums3) |
| 3 | 26 | | { |
| 3 | 27 | | var result = new List<int>(); |
| 3 | 28 | | var seen = new HashSet<int>(); |
| | 29 | |
|
| 3 | 30 | | var set1 = new HashSet<int>(nums1); |
| 3 | 31 | | var set2 = new HashSet<int>(nums2); |
| 3 | 32 | | var set3 = new HashSet<int>(nums3); |
| | 33 | |
|
| 23 | 34 | | foreach (var num in set1) |
| 7 | 35 | | { |
| 7 | 36 | | if (!set2.Contains(num) && !set3.Contains(num)) |
| 3 | 37 | | { |
| 3 | 38 | | continue; |
| | 39 | | } |
| | 40 | |
|
| 4 | 41 | | if (seen.Add(num)) |
| 4 | 42 | | { |
| 4 | 43 | | result.Add(num); |
| 4 | 44 | | } |
| 4 | 45 | | } |
| | 46 | |
|
| 21 | 47 | | foreach (var num in set2) |
| 6 | 48 | | { |
| 6 | 49 | | if (!set3.Contains(num) || set1.Contains(num)) |
| 5 | 50 | | { |
| 5 | 51 | | continue; |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | if (seen.Add(num)) |
| 1 | 55 | | { |
| 1 | 56 | | result.Add(num); |
| 1 | 57 | | } |
| 1 | 58 | | } |
| | 59 | |
|
| 3 | 60 | | return result; |
| 3 | 61 | | } |
| | 62 | | } |