Skip to content

Instantly share code, notes, and snippets.

@lndgalante
Created April 1, 2024 15:21
Show Gist options
  • Save lndgalante/2743fda27b8a14ead1bb753c5ae287a7 to your computer and use it in GitHub Desktop.
Save lndgalante/2743fda27b8a14ead1bb753c5ae287a7 to your computer and use it in GitHub Desktop.
Given an array of numbers, add all of the values together but only if the number doesn't repeat a digit.
function hasRepeatedDigits(value: number): boolean {
const stringValue = String(value);
return new Set(stringValue).size !== stringValue.length;
}
function uniqueSum(values: number[]): number {
return values.reduce((accumulator, value) => hasRepeatedDigits(value) ? accumulator : accumulator + value, 0)
}
console.log(uniqueSum([1, 2, 3])) // 6
console.log(uniqueSum([11, 22, 33])) // 0
console.log(uniqueSum([101, 2, 3])) // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment