From 9c3b827af7f00b1d7906a6352044843f09473886 Mon Sep 17 00:00:00 2001 From: hellache <72650746+hellache@users.noreply.github.com> Date: Sun, 18 Oct 2020 00:50:41 +0530 Subject: [PATCH] Create Find the Duplicate Number.java --- .../Find the Duplicate Number.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Problem Solving/Find the Duplicate Number.java diff --git a/Problem Solving/Find the Duplicate Number.java b/Problem Solving/Find the Duplicate Number.java new file mode 100644 index 0000000..fc880c6 --- /dev/null +++ b/Problem Solving/Find the Duplicate Number.java @@ -0,0 +1,24 @@ +/** + * Solution to Find the Duplicate Number at LeetCode in Java + * + * author: hellache + * ref: https://leetcode.com/problems/find-the-duplicate-number/ + */ +class Solution { + public int findDuplicate(int[] nums) { + int p1=nums[0]; + int p2=nums[0]; + p1=nums[p1]; + p2=nums[nums[p2]]; + while(p1!=p2){ + p1=nums[p1]; + p2=nums[nums[p2]]; + } + p1=nums[0]; + while(p1!=p2){ + p1=nums[p1]; + p2=nums[p2]; + } + return p1; + } +}