# Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:
Describe your issue in detail here.

Giving expected output but I am not able to pass the test. Can anyone please guide me what I am doing wrong here. I know there is other way to solve this problem but its my own algorithm and trying to learn by practice.

``````function diffArray(arr1, arr2) {
const newArr = [];
let arr = arr1.concat(arr2);
let output = arr
.reduce(function(acc,curr){
if(acc[curr]){
acc[curr] = ++acc[curr];
}else{
acc[curr] =1;
}
return acc;
},{});

//console.log(output);
for (const keys in output){
if(output[keys]==1){
newArr.push(String(keys));
}
}
return newArr;
}

console.log(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]));
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36`

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Does this line do what you think it does?

Which tests aren’t passing?

acc[curr] = ++acc[curr];

This code increment the counter; same as acc[curr] = acc[curr} + 1;

The following three test cases are not passing even though i am getting the same output:

`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return `[4]`

`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return `["piglet", 4]`

`[1, "calf", 3, "piglet"], [7, "filly"]` should return `[1, "calf", 3, "piglet", 7, "filly"]`

fyi: I managed to complete this test using simple declarative algorithm, but i want to know why my this approch did not worked…

It isn’t the same. It gets the same results, but in a roundabout way. I’d check how that line works.

Here you are making everything a string, but not all array contents are supposed to be a string.

The output value of numbers are in string, that might be the reasons that is not allowing to pass the test.
However, I have removed the ‘String’ from this line of code, ‘newArr.push(String(keys));’ but its giving the same result, I even try to convert to Integer but the string items giving NaN and there is no way to test if its number or string because all the keys are in string . So I think its not right approch to solve the problem.

Object keys are always strings, so you are stuck there. I don’t see a clean way around that.

Try validate whether the key can be parseInt successfully?

``````if(output[keys]==1 && Number.isInteger(parseInt(keys))){
newArr.push(parseInt(keys));
else if ...
``````

I’d put that in the category of “coding to the test” though. Integer and String aren’t the only two types.

1 Like

@JeremyLT I found a JS method isNaN(), with that use I am able to solve the problem.

for (const keys in output){
if(output[keys]==1){
if(!isNaN(keys))
newArr.push(parseInt(keys))
else{
newArr.push(String(keys));
}
}
}

And with that I am able to pass all the test. Thanks for all the support and recommendation.
Best Regards,
P

It works for these tests, but I’d consider if you can think of an alternative approach or a modification. This won’t work if the data is a boolean, for example.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.