From 8dd4cacd7db3a782b9cf5766cc78cc27604335af Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 24 Mar 2024 17:06:12 +0530 Subject: [PATCH] Create 287. Find the Duplicate Number1 --- 287. Find the Duplicate Number1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 287. Find the Duplicate Number1 diff --git a/287. Find the Duplicate Number1 b/287. Find the Duplicate Number1 new file mode 100644 index 0000000..b325ac4 --- /dev/null +++ b/287. Find the Duplicate Number1 @@ -0,0 +1,23 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + int ind = 0; + + // sort the vector + sort(nums.begin(),nums.end()); + for(int i = 0; i < nums.size() - 1; i++) + { + // if two consecutive elements are equal + // you have find a duplicate + // break the loop + if(nums[i] == nums[i+1]) + { + ind = nums[i]; + break; + } + } + // return duplicate value + return ind; + } + // for github repository link go to my profile. +};