Created
January 2, 2026 19:23
-
-
Save tatsuyax25/3c4f0edc3c7d3db53922c5d52d7887f3 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var repeatedNTimes = function(nums) { | |
| // We'll use a Set to track which numbers we've seen. | |
| const seen = new Set(); | |
| // Walk through each number in the array | |
| for (let num of nums) { | |
| // If we've seen this number before, it must be the one | |
| // that is repeated n times (because only one element repeats). | |
| if (seen.has(num)) { | |
| return num; | |
| } | |
| // Otherwise, record it as seen and continue. | |
| seen.add(num); | |
| } | |
| // Given the problem guarantees, we should always have returned | |
| // inside the loop. This is just a fallback. | |
| return -1; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment