Easy Java solution with O(2n), any way to improve?


  • 0
    A
    public class Solution {
        public int findDuplicate(int[] nums) {
            int n = nums.length;
            int[] flag = new int[n];
            for (int i = 0; i < n; ++i) {
                flag[i] = 0;
            }
            for (int j = 0; j < n; ++j) {
                if (flag[nums[j]] == 0)
                    flag[nums[j]] = 1;
                else
                    return nums[j];
            }
            return -1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.