| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 sealed class TwoOutOfThreeFrequencyArray : ITwoOutOfThree |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 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 frequencyArray = new int[100]; |
| | | 28 | | |
| | 27 | 29 | | foreach (var num1 in nums1) |
| | 9 | 30 | | { |
| | 9 | 31 | | var num1Index = num1 - 1; |
| | | 32 | | |
| | 9 | 33 | | if (frequencyArray[num1Index] != 0) |
| | 2 | 34 | | { |
| | 2 | 35 | | continue; |
| | | 36 | | } |
| | | 37 | | |
| | 7 | 38 | | frequencyArray[num1Index] = 1; |
| | 7 | 39 | | } |
| | | 40 | | |
| | 3 | 41 | | var result = new List<int>(); |
| | | 42 | | |
| | 23 | 43 | | foreach (var num2 in nums2) |
| | 7 | 44 | | { |
| | 7 | 45 | | var num2Index = num2 - 1; |
| | | 46 | | |
| | 7 | 47 | | switch (frequencyArray[num2Index]) |
| | | 48 | | { |
| | | 49 | | case 1: |
| | 3 | 50 | | result.Add(num2); |
| | | 51 | | |
| | 3 | 52 | | frequencyArray[num2Index] = -1; |
| | 3 | 53 | | break; |
| | | 54 | | case 0: |
| | 3 | 55 | | frequencyArray[num2Index] = 2; |
| | 3 | 56 | | break; |
| | | 57 | | } |
| | 7 | 58 | | } |
| | | 59 | | |
| | 17 | 60 | | foreach (var num3 in nums3) |
| | 4 | 61 | | { |
| | 4 | 62 | | var num3Index = num3 - 1; |
| | | 63 | | |
| | 4 | 64 | | if (frequencyArray[num3Index] != 1 && frequencyArray[num3Index] != 2) |
| | 2 | 65 | | { |
| | 2 | 66 | | continue; |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | result.Add(num3); |
| | | 70 | | |
| | 2 | 71 | | frequencyArray[num3Index] = -1; |
| | 2 | 72 | | } |
| | | 73 | | |
| | 3 | 74 | | return result; |
| | 3 | 75 | | } |
| | | 76 | | } |