| | 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 TwoOutOfThreeFrequencyDictionary : 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 frequencyDictionary = new Dictionary<int, int>(); |
| | 28 | |
|
| 16 | 29 | | foreach (var num1 in new HashSet<int>(nums1).Where(num1 => !frequencyDictionary.TryAdd(num1, 1))) |
| 0 | 30 | | { |
| 0 | 31 | | frequencyDictionary[num1]++; |
| 0 | 32 | | } |
| | 33 | |
|
| 21 | 34 | | foreach (var num2 in new HashSet<int>(nums2).Where(num2 => !frequencyDictionary.TryAdd(num2, 1))) |
| 3 | 35 | | { |
| 3 | 36 | | frequencyDictionary[num2]++; |
| 3 | 37 | | } |
| | 38 | |
|
| 19 | 39 | | foreach (var num3 in new HashSet<int>(nums3).Where(num3 => !frequencyDictionary.TryAdd(num3, 1))) |
| 3 | 40 | | { |
| 3 | 41 | | frequencyDictionary[num3]++; |
| 3 | 42 | | } |
| | 43 | |
|
| 3 | 44 | | var result = new List<int>(); |
| | 45 | |
|
| 31 | 46 | | foreach (var frequency in frequencyDictionary) |
| 11 | 47 | | { |
| 11 | 48 | | if (frequency.Value < 2) |
| 6 | 49 | | { |
| 6 | 50 | | continue; |
| | 51 | | } |
| | 52 | |
|
| 5 | 53 | | result.Add(frequency.Key); |
| 5 | 54 | | } |
| | 55 | |
|
| 3 | 56 | | return result; |
| 3 | 57 | | } |
| | 58 | | } |