< Summary

Information
Class: LeetCode.Algorithms.IntersectionOfTwoArrays2.IntersectionOfTwoArrays2Sorting
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IntersectionOfTwoArrays2\IntersectionOfTwoArrays2Sorting.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Intersect(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IntersectionOfTwoArrays2\IntersectionOfTwoArrays2Sorting.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.IntersectionOfTwoArrays2;
 13
 14/// <inheritdoc />
 15public class IntersectionOfTwoArrays2Sorting : IIntersectionOfTwoArrays2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n + m log m)
 19    ///     Space complexity - O(n + m)
 20    /// </summary>
 21    /// <param name="nums1"></param>
 22    /// <param name="nums2"></param>
 23    /// <returns></returns>
 24    public int[] Intersect(int[] nums1, int[] nums2)
 225    {
 226        Array.Sort(nums1);
 227        Array.Sort(nums2);
 28
 229        var result = new List<int>();
 30
 231        var i = 0;
 232        var j = 0;
 33
 1134        while (i < nums1.Length && j < nums2.Length)
 935        {
 936            if (nums1[i] < nums2[j])
 337            {
 338                i++;
 339            }
 640            else if (nums1[i] > nums2[j])
 241            {
 242                j++;
 243            }
 44            else
 445            {
 446                result.Add(nums1[i]);
 47
 448                i++;
 449                j++;
 450            }
 951        }
 52
 253        return [.. result];
 254    }
 55}