본문 바로가기

알고리즘/LeetCode

LeetCode | 287 | Find the Duplicate Number

 

import java.util.*;

class Solution {
    public int findDuplicate(int[] nums) {
        Arrays.sort(nums);
        
        for (int i=0;i<nums.length-1;i++){
            if (nums[i] == nums[i+1]){
                return nums[i];
            }
        }
        
        return -1;
    }
}

 

leetcode.com/problems/find-the-duplicate-number/