Created
February 22, 2022 01:33
-
-
Save petercr/1ad701864361123af749456774880042 to your computer and use it in GitHub Desktop.
My Solution To The Lonely Interger Problem in JavaScript
This file contains hidden or 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
/* | |
* Complete the 'lonelyinteger' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts INTEGER_ARRAY a as parameter. | |
*/ | |
function lonelyinteger(a) { | |
// If a is only 1 int long just return it | |
if(a.length == 1) { | |
return a; | |
} else { | |
// outer for loop to check values with | |
outer: for( let i=0; i<a.length; i++) { | |
// inner for loop to check values with | |
inner: for (let j=0; j<a.length; j++) { | |
if(i===j) { | |
continue inner; | |
} else { | |
if( a[i] === a[j]) { | |
continue outer; | |
} | |
} | |
} | |
return a[i]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment