N-Repeated Element in Size 2N Array - Complete Solution Guide
N-Repeated Element in Size 2N Array is LeetCode problem 961, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array nums with the following properties: nums.length == 2 * n . nums contains n + 1 unique elements. Exactly one element of nums is repeated n times. Return the element that is repeated n times . Example 1: Input: nums = [1,2,3,3] Output: 3 Example 2: Input: nums = [2,1,2,5,3,2] Output: 2 Example 3: Input: nums = [5,1,5,2,5,3,5,4] Output: 5 Constraints: 2 <= n <= 5000 nums.length == 2 * n 0 <= nums[i] <= 10 4 nums contains n + 1 unique elements and one of them is repeat
Detailed Explanation
The problem asks us to find the element that appears N times in an array 'nums' of size 2N. The array contains n+1 unique elements, meaning one element is repeated exactly n times. The input is an integer array 'nums', and the output is the integer that is repeated N times.
Solution Approach
The Python3 solution uses a hash table (dictionary) to store the counts of each number encountered in the input array `nums`. It iterates through the array, and for each number, it checks if the number is already present in the hash table. If it is, it means we have found the element that is repeated N times, and we return it. Otherwise, we add the number to the hash table with a count of 1. The Java/C/C++ solutions try to detect repeated elements by comparing adjacent elements and elements two indices apart. Since the repeated number appears n times and the array has 2*n size, the repeated number will likely appear adjacent or with one number in between. The algorithm returns the first number found this way.
Step-by-Step Algorithm
- Step 1: Initialize an empty hash table (dictionary) to store the counts of each element.
- Step 2: Iterate through the input array `nums`.
- Step 3: For each element `num` in `nums`:
- Step 4: Check if `num` is present as a key in the hash table.
- Step 5: If `num` is present, return `num` as it is the element repeated N times.
- Step 6: If `num` is not present, add `num` as a key to the hash table with a value of 1 (representing its count).
- Step 7: The algorithm will return the result within the loop. If it reaches the end of the array without finding a repeated number using Python, that means there is an issue with the given array (but that would contradict with the problem statement). The C/Java/C++ solutions will return the first element if no adjacent elements are found.
Key Insights
- Insight 1: Since only one element is repeated N times, and there are n+1 unique elements in the array, the repeated element is guaranteed to be present.
- Insight 2: The repeated element must occur at least twice in the array since n >= 2.
- Insight 3: A simple hash table (dictionary) can be used to efficiently count the occurrences of each element.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: Akamai.