# Sock merchant from hackerrank - idiomatic typescript?

## Sock Merchant

John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

For example, there are $$\ n = 7\$$ socks with colors $$\ar = [1,2,1,2,1,3,2]\$$. There is one pair of color $$\1\$$ and one of color $$\2\$$. There are three odd socks left, one of each color. The number of pairs is $$\2\$$.

### Function Description

Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

sockMerchant has the following parameter(s):

• n: the number of socks in the pile
• ar: the colors of each sock

### Input Format

The first line contains an integer $$\n\$$, the number of socks represented in $$\ar\$$.
The second line contains $$\n\$$ space-separated integers describing the colors $$\ar[i]\$$ of the socks in the pile.

I just started learning typescript, and I don't know if this is good style:

import * as fs from 'fs';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputStringTemp = '';
let inputString: string[] = [];
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputStringTemp += inputStdin;
});

process.stdin.on('end', () => {
inputString = inputStringTemp.replace(/\s*$/, '') .split('\n') .map(str => str.replace(/\s*$/, ''));

main();
});

return inputString[currentLine++];
}

function sockMerchant(n: number, ar: number[]): number {
// https://stackoverflow.com/questions/21070401/how-does-the-hash-variable-syntax-work-in-typescript/21071089#21071089
// https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types
// https://stackoverflow.com/questions/44337856/check-if-specific-object-is-empty-in-typescript/44338054
interface NumberMap {
[index: number]: number;
}

// https://stackoverflow.com/questions/45571161/typescript-typing-map-object-and-instantiate-with-empty-object/45571346#45571346
// https://stackoverflow.com/questions/29043279/how-to-use-string-indexed-interface-of-typescript/29043535#29043535
let obj: NumberMap = {} as NumberMap;
for (let key of ar) {
// https://stackoverflow.com/questions/39590858/how-to-increment-a-value-in-a-javascript-object/39591127#39591127
typeof obj[key] === 'undefined' ? obj[key] = 1 : obj[key]++;
}
let count = 0;
// https://stackoverflow.com/questions/37699320/iterating-over-typescript-map/50232058#50232058
for (const [key, value] of Object.entries(obj)) {
// https://stackoverflow.com/questions/4228356/integer-division-with-remainder-in-javascript/4228376#4228376
count += Math.floor(value / 2);
}
return count;
}

function main() {
// https://stackoverflow.com/questions/45194964/how-to-assign-string-undefined-to-string-in-typescript/47553970#47553970
// https://stackoverflow.com/questions/23314806/setting-default-value-for-typescript-object-passed-as-argument/44937766#44937766
// const ws = fs.createWriteStream(process.env.OUTPUT_PATH!);
const ws = fs.createWriteStream(process.env.OUTPUT_PATH || "output.txt");

const ar = readLine().split(' ').map(arTemp => parseInt(arTemp, 10));
let result = sockMerchant(n, ar);
ws.write(result + "\n");

ws.end();
}


https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup

Let's make it pure function:

function readLine() {
return inputString[currentLine++];
}


IMHO both for loop should be transform to reduce, or at least the second one where you use only addition, it would be still simple and readable as a reduce().

Personally, I don't like those shorts like ws - sounds like websocket, ar and means nothing, obj and count are too generic.

result could be declared as const, as well obj and count.

And lastly, too much links as comments, why do you assume reviewer don't know TS, JS... or google? :) Not to mention, the links could be dead soon.