Created
May 10, 2022 15:54
-
-
Save fazeelanizam13/ef8bdf7b9690be1584ba7b28c4907e11 to your computer and use it in GitHub Desktop.
This file contains 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
// 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 findDuplicateNumber (input) { | |
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, return integer | |
if (input[i] === input[j]) return input[i] | |
} | |
} | |
} | |
} else return 'Please input an array of integers.' | |
} | |
console.log(findDuplicateNumber([1, 68, 3, 4, 5, 68, 9, 10])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment