Skip to content

Solution - #1 - Nishitha - 10/05/2025 - #49

Open
nishithadaryn wants to merge 2 commits into
Dijkstra-Edu:masterfrom
nishithadaryn:add-contains-duplicate-cpp
Open

Solution - #1 - Nishitha - 10/05/2025#49
nishithadaryn wants to merge 2 commits into
Dijkstra-Edu:masterfrom
nishithadaryn:add-contains-duplicate-cpp

Conversation

@nishithadaryn

Copy link
Copy Markdown

Problem Explanation

Problem Statement

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.

Approach

  1. Sort the array so that duplicate elements are adjacent.
  2. Iterate through the sorted array and check if any adjacent elements are equal.

Code Explanation

  • sort(nums.begin(), nums.end());: Sorts the array in ascending order.
  • for (int i = 1; i < nums.size(); ++i): Iterates through the array starting from the second element.
  • if (nums[i] == nums[i - 1]): Checks if the current element is equal to the previous one.
  • return true;: Returns true immediately upon finding a duplicate.
  • return false;: If no duplicates are found after the loop, returns false.

@SSexpl SSexpl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a O(NlogN) TC and s O(1) SC , we can have a solution with a better time complexity

@JRS296
JRS296 self-requested a review May 10, 2025 06:09
@JRS296

JRS296 commented May 10, 2025

Copy link
Copy Markdown
Member

this is a O(NlogN) TC and s O(1) SC , we can have a solution with a better time complexity

You can add multiple solutions, as well as their explanations. The idea is to start from a not so optimized solution, and gradually move to an optimized solution. You can follow this for reference: https://github.com/Dijkstra-Edu/LeetCode-Solutions/pull/3/files

Comment on lines +1 to +13
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end()); // O(n log n)
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1]) {
return true;
}
}
return false;
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1 to +12
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end()); // O(n log n)
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1]) {
return true;
}
}
return false;
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the solution above. And not following convention. It should be 1 Solution at a time. Follow the Folder structure.

@JRS296 JRS296 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes need to be made. Please read the readme.md clearly.

@JRS296

JRS296 commented May 10, 2025

Copy link
Copy Markdown
Member

Also the title is still not clear. Kindly have a look at the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants