A golang solution


  • 0
    N
    func intersect(nums1 []int, nums2 []int) []int{
    	var r []int
    	m := make(map[int][]int)
    
    	for _, v := range nums1 {
    		if _, ok := m[v]; !ok {
    			m[v] = make([]int, 2)
    		}
    		m[v][0]++
    	}
    
    	for _, v := range nums2 {
    		if _, ok := m[v]; !ok {
    			m[v] = make([]int, 2)
    		}
    		m[v][1]++
    	}
    
    	for k, v := range m {
    		if v[0] != 0 && v[1] != 0 {
    			if v[0] > v[1] {
    				for i := 0; i < v[1]; i++ {
    					r = append(r, k)
    				}
    			} else {
    				for i := 0; i < v[0]; i++ {
    					r = append(r, k)
    				}
    			}
    		}
    	}
    
    	return r
    }
    

Log in to reply
 

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