From 03aa8750892dc65940e7baf006c423fea718619b Mon Sep 17 00:00:00 2001 From: Shivam Singh <76156804+2000shivam659@users.noreply.github.com> Date: Wed, 28 Jul 2021 07:04:25 +0530 Subject: [PATCH 1/2] Java solution "Find the Duplicate Number " --- .../Java/10_Find_the_Duplicate_Number | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Bit-Manipulation/Java/10_Find_the_Duplicate_Number diff --git a/Bit-Manipulation/Java/10_Find_the_Duplicate_Number b/Bit-Manipulation/Java/10_Find_the_Duplicate_Number new file mode 100644 index 0000000..94fdf26 --- /dev/null +++ b/Bit-Manipulation/Java/10_Find_the_Duplicate_Number @@ -0,0 +1,53 @@ +// Question https://leetcode.com/problems/find-the-duplicate-number/ + +/** + * first of all find the max number in num[], + * then create an array of size (max_nums + 1). + * finally find out the first repetition number using frequency method, + * if frequency of any number == 2 then return false else true. + */ + +// Solution + +import java.util.Scanner; + +class Solution { + public static int findDuplicate(int[] nums) { + int n = -1; + + for(int i = 0; i < nums.length; i++) + n = Math.max(n,nums[i]); + + int[] arr = new int[n+1]; + + for(int i = 0; i < nums.length; i++){ + arr[nums[i]]++; + if(arr[nums[i]] == 2) + return nums[i]; + } + return -1; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int n = sc.nextInt(); + int[] nums = new int[n]; + + for(int i = 0; i < n; i++) + nums[i] = sc.nextInt(); + + System. out.println( findDuplicate(nums) ); + } +} + +// Examples +/** + * Example 1: + * Input: nums = [1,3,4,2,2] + * Output: 2 + * + * Example 2: + * Input: nums = [3,1,3,4,2] + * Output: 3 + */ From 0456762f065c45876c10573c142992a8f60ab57f Mon Sep 17 00:00:00 2001 From: Shivam Singh <76156804+2000shivam659@users.noreply.github.com> Date: Fri, 30 Jul 2021 09:03:09 +0530 Subject: [PATCH 2/2] Rename 10_Find_the_Duplicate_Number to 10_Find_the_Duplicate_Number.java --- ...ind_the_Duplicate_Number => 10_Find_the_Duplicate_Number.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Bit-Manipulation/Java/{10_Find_the_Duplicate_Number => 10_Find_the_Duplicate_Number.java} (100%) diff --git a/Bit-Manipulation/Java/10_Find_the_Duplicate_Number b/Bit-Manipulation/Java/10_Find_the_Duplicate_Number.java similarity index 100% rename from Bit-Manipulation/Java/10_Find_the_Duplicate_Number rename to Bit-Manipulation/Java/10_Find_the_Duplicate_Number.java