R
Reactiflux

Script – 00-09 Jan 27

Script – 00-09 Jan 27

SScript1/27/2022
So I am trying to get value pairs from an array to be pushed into another array This is my code:
function sockMerchant(n, ar) {
ar.sort(function (a, b) {
return a - b;
});

let groups = [];
for (let i = 0, j = 1; i < ar.length; i += 2, j += 2) {
if (ar[i] === ar[j]) {
groups.push(ar.slice(i, i + 2));
} else {
i - 1;
j - 1;
}
}

return groups.length;
}
function sockMerchant(n, ar) {
ar.sort(function (a, b) {
return a - b;
});

let groups = [];
for (let i = 0, j = 1; i < ar.length; i += 2, j += 2) {
if (ar[i] === ar[j]) {
groups.push(ar.slice(i, i + 2));
} else {
i - 1;
j - 1;
}
}

return groups.length;
}
So this works fine for some values but when using sockMerchant(20, [4, 5, 5, 5, 6, 6, 4, 1, 4, 4, 3, 6, 6, 3, 6, 1, 4, 5, 5, 5]) it returns 8 instead of 9. I then discovered the problem is it skipping the index pairs that are not the same so I decided to add i-1, j-1 so it goes back one index. But that didn't work Any help?
UUUnknown User1/27/2022
2 Messages Not Public
Sign In & Join Server To View
SScript1/27/2022
oh shit my bad <:pepe_laugh_1:932725391079133204> thank u
UUUnknown User1/27/2022
4 Messages Not Public
Sign In & Join Server To View
SScript1/27/2022
!code
UUUnknown User1/27/2022
2 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

Script – 00-09 Jan 27

Join Server