C# Solution


  • 0
    M
    public int[] Intersect(int[] nums1, int[] nums2) 
    {
        IList<int> set1 = new List<int>(); 
        IList<int> set2 = new List<int>(); 
        IList<int> result = new List<int>();         
                
        for (int i = 0; i < nums1.Length; i++)
        {
            set1.Add(nums1[i]);
        }
        
        for (int i = 0; i < nums2.Length; i++)
        {
            set2.Add(nums2[i]);
        }
        
        IList<int> small; 
        IList<int> large; 
        
        
        if (nums1.Length > nums2.Length)
        {            
            large = set1; 
            small = set2; 
        }
        else 
        {
            large = set2;
            small = set1;            
        }
        
        for (int i = 0; i < small.Count; i++)
        {
            if (large.Contains(small[i])) 
            {
                large.RemoveAt(large.IndexOf(small[i])); 
                result.Add(small[i]);                
            }
        }                
        
        return result.ToArray();
    }

Log in to reply
 

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