Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. 1. i. Description of the problem ii. Algorithm of the...


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;
}
};



Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here