First Duplicate

First Duplicate

Example:

Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, return -1.

  • For a = [2, 1, 3, 5, 3, 2], the output should be
  • firstDuplicate(a) = 3.
  • There are 2 duplicates: numbers 2 and 3. The second occurrence of 3 has a smaller index than the second occurrence of 2 does, so the answer is 3.
  • For a = [2, 4, 3, 5, 1], the output should be
  • firstDuplicate(a) = -1.

Algorithm:

1 Iterate over array elements, if array value - 1 which corresponds to a valid index, contains a negative value, then it has already been encountered, therefore return the array value as the first duplicate value encountered. If not, assign the value corresponding to array value -1 index to be equal negation of the existing value, so that when we encounter its duplicate, the above if condition of array value - 1 will be negative and hence the first duplicate value is duly returned.

2 Another solution would be to keep adding the elements in the array to a hashset and upon encountering an already existing element to be added, instead of adding it we return the duplicate element. While both techniques incur a time complexity of T O(n), there is an extra space complexity of S O(n) in the case of hashset solution.

Test:

1 Test with all unique elements in the array.

2 Test with an array with a singular element

3 Test with multiple duplicate elements in the input array.

Solution:

int firstDuplicate(int[] a) {
    for(int i=0;i<a.length;i++)
        if(a[Math.abs(a[i])-1]<0)
            return Math.abs(a[i]);
        else{
            a[Math.abs(a[i])-1]=-a[Math.abs(a[i])-1];
        }
    return -1;
}

// T O(n) S O(1)

int firstDuplicate(int[] a) {
HashSet<Integer> set = new HashSet<>();
    
    for (int i  = 0; i < a.length; i++ ) {
        if (set.contains(a[i])) {
            return a[i];
        }
        set.add(a[i]);
    }
    return -1;
}

// T O(n) S O(n)

//https://gist.github.com/harycane/aa034fdb4f7ddeed566554f37d36543d

Complexity Analysis:

Time complexity in both the solutions are T O(n), but in the case of using a hashset there is an extra space complexity of S O(n).


要查看或添加评论,请登录

Hary Krishnan的更多文章

  • Missing Number

    Missing Number

    Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is…

  • Reorder data in log files

    Reorder data in log files

    https://leetcode.com/problems/reorder-data-in-log-files/description/ You have a list of logs.

    1 条评论
  • Shortest Distance

    Shortest Distance

    https://leetcode.com/problems/shortest-word-distance/ Given a list of words and two words word1 and word2, return the…

  • Shortest Distance II

    Shortest Distance II

    https://leetcode.com/problems/shortest-word-distance-ii/description/ https://gist.

  • Nested List Weight Sum I & II

    Nested List Weight Sum I & II

    https://leetcode.com/problems/nested-list-weight-sum/description/ https://leetcode.

  • Sliding Window Maximum

    Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very…

  • Permutations

    Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3],…

  • Gas Station

    Gas Station

    Example: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a…

  • Group Anagrams

    Group Anagrams

    Example: Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat"…

    1 条评论
  • Maximum Subarray

    Maximum Subarray

    Example: Given an integer array nums, find the contiguous subarray (containing at least one number) which has the…

社区洞察

其他会员也浏览了