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
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