Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Lonely integer problem : Code not working in JS

joji

New Coder
I am trying to resolve a hackerrank challenge in javascript and even if my code is logic it is giving me the wrong answer each time.. Here is my code :

function lonelyinteger(a) {
// Write your code here
if (a.length==1){return a[0];}
if (a.length>1 && a.length<100) {
for(let i = 0; i <=a.length; i++){
for(let j=0;j<=a.length && j!=i;j++){
if (a==a[j]){
a.splice(j,1);
a.splice(i,1);}
}
}return a[0];

}

}

The lonelyInteger function takes an array of integer (in which there are elements duplicated ounce and its length is odd number + this array always contains only one element which is not duplicated) and rreturns the non duplicated element. My approach is to check all the duplicates --> remove them from the array --> Result : my array contains only the non duplicated element in the end so I return it

This is not working for me, where is the problem can you help me please ?

Thanks in advance !

I runned the code many times, evethough the length of the returned list is not 1 as expected means the splice function is not doing the job properly
 
And I don't like
if (a==a[j])
Comparing the entire array with its first element ? This will either return always false, or more likely gives an error - did you check console output ?

I also don't like that you loop through the array, based on its length, and then inside the loop remove remove elements from the array so that its length changes. Maybe it works but I would not rely on JS re-evaluating the length with every iteration. I you really want to do it this way at least work on a copy of the array.

Personally I would sort the array, then loop through it once and find the lone number that has different numbers on each side of it (first and last number of course have to be checked separately). If you are also required to find the index of the lone integer this does not work of course - but nor does your solution.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom