Given an integer array nums, return true if any value appearsat least twice in the array, and return false if every element is distinct.
1.
i. Description of the problem
ii. Algorithm of the following codes (both methods)
iii. Analyze the running time of each step of the algorithms (both methods)
Method 1:
class Solution {public:bool containsDuplicate(vector& nums) {int n= nums.size();sort(begin(nums), end(nums));for (int i=1; i{if (nums[i] == nums[i-1]){return true;}}return false;}};Method 2:class Solution {public:bool containsDuplicate(vector& nums) {int n = nums.size();set s;for (int i=0; i{if (s.count(nums[i])){return true;}s.insert(nums[i]);}return false;}};
Method 2:
class Solution {public:bool containsDuplicate(vector& nums) {int n = nums.size();set s;for (int i=0; i{if (s.count(nums[i])){return true;}s.insert(nums[i]);}return false;}};
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here