C# Solution


  • 0
    M
    public int[] Intersection(int[] nums1, int[] nums2) {
        HashSet<int> set1 = new HashSet<int>(); 
        HashSet<int> set2 = new HashSet<int>();         
        
        for (int i = 0; i < nums1.Length; i++)
        {
            if (!set1.Contains(nums1[i])) set1.Add(nums1[i]);
        }
        
        for (int i = 0; i < nums2.Length; i++)
        {
            if (!set2.Contains(nums2[i])) set2.Add(nums2[i]);
        }
        
        IList<int> result = new List<int>(); 
        
        foreach (var item in set2)
        {
            if (set1.Contains(item)) result.Add(item);
        }        
        
        return result.ToArray(); 
    }

Log in to reply
 

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