C# O(N) using dictionary, code is too long, could be more clear


  • 0
    Y

    public class Solution {
    public int[] Intersection(int[] nums1, int[] nums2) {

        var hashInt = new Dictionary<int, int>();
        var hashResultInt = new Dictionary<int, int>();
        
        int minLength = Math.Min(nums1.Length, nums2.Length);
        
        if(minLength == 0)
        {
            return new int[0];
        }
        
        var result=new List<int>();
        
        if(nums1.Length < nums2.Length)
        {
            for(int i = 0; i < nums1.Length; i++)
            {
                if(!hashInt.ContainsKey(nums1[i]))
                {
                    hashInt.Add(nums1[i],nums1[i]);
                }
            }
            
            for(int i = 0; i < nums2.Length; i++)
            {
                if(hashInt.ContainsKey(nums2[i]))
                {
                    if(!hashResultInt.ContainsKey(nums2[i]))
                    {
                        hashResultInt.Add(nums2[i],nums2[i]);
                    }
                }
            }
        }
        else
        {
            for(int i = 0; i < nums2.Length; i++)
            {
                if(!hashInt.ContainsKey(nums2[i]))
                {
                    hashInt.Add(nums2[i],nums2[i]);
                }
            }
            
            for(int i = 0; i < nums1.Length; i++)
            {
                if(hashInt.ContainsKey(nums1[i]))
                {
                    if(!hashResultInt.ContainsKey(nums1[i]))
                    {
                        hashResultInt.Add(nums1[i],nums1[i]);
                    }
                }
            }
        }
        
        return hashResultInt.Keys.ToArray();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.