Script
Script3y ago

Script – 00-09 Jan 27

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?
7 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
Script3y ago
oh shit my bad <:pepe_laugh_1:932725391079133204> thank u
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/936050337557676124
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
Script3y ago
!code
reactibot
reactibot3y ago
Attaching Code
```js // short code snippets go here ``` Link a Gist to upload entire files: https://gist.github.com Link a Code Sandbox to share runnable examples: https://codesandbox.io/s Link a Code Sandbox to an existing GitHub repo: https://codesandbox.io/s/github/<username>/<reponame> Link a TypeScript Playground to share types: https://www.typescriptlang.org/play Link a Snack to share React Native examples: https://snack.expo.io
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/936050337557676124 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26