Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created January 2, 2026 19:23
Show Gist options
  • Select an option

  • Save tatsuyax25/3c4f0edc3c7d3db53922c5d52d7887f3 to your computer and use it in GitHub Desktop.

Select an option

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.
/**
* @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