217. Duplicate elements exist | Contains Duplicate

Source: Internet
Author: User

Given an array of integers, find if the array contains any duplicates.

Your function should return TRUE if any value appears at least twice in the array, and it should return FALSE if every ele ment is distinct.

Example 1:

Input: [1,2,3,1]output:true

Example 2:

Input: [1,2,3,4]output:false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]output:true

Given an array of integers, determine if there are duplicate elements.

If any value occurs at least two times in the array, the function returns True. Returns False if each element in the array is not the same.

Example 1:

Input: [1,2,3,1] Output: True

Example 2:

Input: [1,2,3,4] Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2] Output: True
1 classSolution {2Func Containsduplicate (_ Nums: [Int])Bool {3         ifNums = = Nil | | Nums.count = =0{return false}4         varArray:[int] =Nums5         //Sort First6Array.Sort () {$0< $1}7         //then traverse8          forIinch 0.. < (array.count-1)9         {Ten             ifArray[i] = = array[i+1] One             { A                 return true -             } -         } the         return false -     } -}

36ms

1 classSolution {2Func Containsduplicate (_ Nums: [Int])Bool {3         var Set= set<int>()4         5          forIndexinchNums.indiceswhere!Set. Insert (Nums[index]). inserted{6             return true7         }8         return false9     }Ten}

40ms

1 class Solution {2     func containsduplicate (_ Nums: [Int]), Bool {3         var  Set = Set (nums)4         returnset. Count! = Nums.count  5    }6 }

217. Duplicate elements exist | Contains Duplicate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.