From a56a328d6b041980f7bf2fdc4cead982454e4870 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 24 Mar 2024 10:59:12 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB287.=20Find=20the=20Duplicate?= =?UTF-8?q?=20Number=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/find-the-duplicate-number.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cs/find-the-duplicate-number.cs diff --git a/cs/find-the-duplicate-number.cs b/cs/find-the-duplicate-number.cs new file mode 100644 index 0000000..a16ddfa --- /dev/null +++ b/cs/find-the-duplicate-number.cs @@ -0,0 +1,19 @@ +public class Solution { + public int FindDuplicate(int[] nums) { + int slow = nums[0]; + int fast = nums[0]; + + do { + slow = nums[slow]; + fast = nums[nums[fast]]; + } while (fast != slow); + + slow = nums[0]; + while (slow != fast) { + slow = nums[slow]; + fast = nums[fast]; + } + + return slow; + } +}