Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java solution "Find the Duplicate Number " #238

Merged
merged 2 commits into from
Jul 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Bit-Manipulation/Java/10_Find_the_Duplicate_Number.java
Original file line number Diff line number Diff line change
@@ -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
*/