Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
Created May 10, 2022 15:54
Show Gist options
  • Save fazeelanizam13/4e9193d6488f5c451ffb57b45f2da884 to your computer and use it in GitHub Desktop.
Save fazeelanizam13/4e9193d6488f5c451ffb57b45f2da884 to your computer and use it in GitHub Desktop.
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
else return false
}
return true
// if not an array
} else return false
}
function findDuplicateNumbers (input) {
let duplicates = []
if (isIntegerArray(input)) {
// for integers being compared
for (let i = 0; i < input.length; i++) {
// for integers being compared with
for (let j = 0; j < input.length; j++) {
// if not comparing to self
if (!(i === j)) {
// AND if found similar with each other,
// AND if isn't already in 'duplicates' array
// push into 'duplicates' array
if (
(input[i] === input[j]) &&
!duplicates.includes(input[i])
) duplicates.push(input[i])
}
}
}
return duplicates
} else return 'Please input an array of integers.'
}
console.log(findDuplicateNumbers([1, 68, 3, 4, 5, 68, 9, 1, 1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment