Concise c# solution (2-line)


  • 2
    M
    public class Solution {
        public int[] Intersect(int[] nums1, int[] nums2) {
            var map1 = nums1.GroupBy(n => n).ToDictionary(g => g.Key, g => g.Count());
            return nums2.Where(n => map1.ContainsKey(n) && map1[n]-- > 0).ToArray();
        }
    }

  • 1
    F

    can you explain it?


  • 0
    M

    First we create a map "map1" with the distinct numbers as keys and their occurrences in array nums1. Then we pick the elements from nums2 that have appeared in nums1.


  • 0
    F

    @zhenlilu2003 said in Concise c# solution (2-line):

    First we create a map "map1" with the distinct numbers as keys and their occurrences in array nums1. Then we pick the elements from nums2 that have appeared in nums1.
    3Q a


Log in to reply
 

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