Skip to content

Instantly share code, notes, and snippets.

@mooeypoo
Last active May 10, 2021 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooeypoo/1f65202edc5499cb9689be33e72acf17 to your computer and use it in GitHub Desktop.
Save mooeypoo/1f65202edc5499cb9689be33e72acf17 to your computer and use it in GitHub Desktop.
Given an integer n, return true if n^3 and n have the same set of digits.
// Submission for "rendezvous with cassidoo" interview question of the week
// Subscribe to the newsletter - https://buttondown.email/cassidoo
// NOTE: This isn't the most efficient code... but I had to come up with a smartass method ;)
// Not sure if those will pass me that interview question -- but they're fun to make!
const sameDigits = num => {
const removeDupes = arr => {
return arr
.filter((num, index) => arr.indexOf(num) === index)
}
// Remove duped digits
const digitsForNum = removeDupes(String(num).split(''))
const digitsForNum3 = removeDupes(String(num*num*num).split(''))
// Build comparison - biggest first
const comparison = digitsForNum.length > digitsForNum3 ?
[digitsForNum, digitsForNum3] : [digitsForNum3, digitsForNum];
// Compare digits - remove digits that are the same between the two
// If any digits are left, the comparison fails
return !comparison[0]
.filter(num => comparison[1].indexOf(num) === -1).length
}
// Test
// $ sameDigits(1) // true
// $ sameDigits(10) // true
// $ sameDigits(251894) // true
// $ sameDigits(251895) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment