当前位置:网站首页>Array union set

Array union set

2022-06-10 22:11:00 Morris_

Find the union of two arrays

  • Swift
        let nums1: [Int] = [1, 2, 2, 3, 4]
        let nums2: [Int] = [2, 3, 2]
        let nums = union(nums1, nums2)
        print(nums)
    func union(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
    
        
        var nums: [Int] = []
        
        for i in 0..<nums1.count {
    
            
            for j in 0..<nums2.count {
    
                
                if nums1[i] == nums2[j] {
    
                    if nums.contains(nums1[i]) {
    

                    }
                    else {
    
                        nums.append(nums1[i])
                    }
                    break
                }
            }
        }
        
        return nums
    }

 Insert picture description here

原网站

版权声明
本文为[Morris_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102049515431.html