Skip to content

Instantly share code, notes, and snippets.

@thunder775
Last active June 2, 2022 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thunder775/26d585711c061dbd2780c67f44e6ac24 to your computer and use it in GitHub Desktop.
Save thunder775/26d585711c061dbd2780c67f44e6ac24 to your computer and use it in GitHub Desktop.
/**
* You are given two non-empty arrays `a` and `b`, each consisting of objects of
* type Entry (see the type declaration below)
* Each 'label' is unique within an array (e.g. you cannot have two entries
* with `label` 'a' within the same array).
*
* Write a function `merge` which does the following:
* 1) Merges the contents of the two arrays together and returns a new array.
* When two entries have the same `label`, the result should have
* their `value` property summed up.
*
* Each `label` must remain unique within the resulting array.
* E.g. { label: 'a', value: 1 } and { label: 'a', value: 2 }
* should be combined into { label: 'a', value: 3 }
*
* 2) Sorts the resulting array by the `label` property in ascending order
* (you can use standard JS string comparison)
*
* For example:
* [
* { label: 'Abc', value: 1 },
* { label: 'b', value: 1 },
* { label: 'a', value: 1 }
* ]
* should become
* [
* { label: 'Abc', value: 1 },
* { label: 'a', value: 1 },
* { label: 'b', value: 1 }
* ]
*
* To simplify the solition, you can assume the following:
* - Both arrays length 1 =< length <= 10000
* - `label` is a non-empty string consinsting of letters of English alphabet (a-z A-Z)
* - `value` is a non-negative number (value >= 0)
* - The order of elements' `labels` within the array is random (see example)
* - Each 'label' is unique within an array (you cannot have two entries
* with `label` 'a' within the same array).
*
* For any questions & submissions - please send an email to develop@grantit.io
*
* Requirements for submission:
* - Please specify your name in the email subject
* - Attach your submission. You can make a .txt / .js file attachment to your email
* or provide a link to a public codesandbox / codepen / replit / codeshare.
*
* If your code has several iterations - feel free to leave all of them within
* the same file. That will help us to understand your thought process better.
*/
type Entry = {
label: string;
value: number;
};
const merge = (array1: Entry[], array2: Entry[]): Entry[] => {
const entriesMapByLabel: {[key: string]: number} = {};
for (let {label, value} of [...array1, ...array2]) {
entriesMapByLabel[label] = entriesMapByLabel[label] && entriesMapByLabel[label] + value || value;
}
const mergedEntries: Entry[] = Object.keys(entriesMapByLabel).map(key => ({label: key, value: entriesMapByLabel[key]}));
mergedEntries.sort(entriesCompareFunction)
return mergedEntries
};
function entriesCompareFunction(a: Entry, b: Entry): number {
const [label1, label2]: Array<string> = [a.label, b.label]
if (label1 < label2) return -1
if (label2 < label1) return 1
return 0
}
// /** Use the following code to test your submission.
const a: Entry[] = [
{label: 'a', value: 1},
{label: 'b', value: 1},
{label: 'c', value: 1},
{label: 'e', value: 5},
];
const b: Entry[] = [
{label: 'Abc', value: 100},
{label: 'e', value: 0},
{label: 'a', value: 2},
{label: 'c', value: 1},
{label: 'd', value: 1},
];
const result = merge(a, b);
console.log(result);
// console.log('A' < 'a')
// The result should be
// [
// { label: 'Abc', value: 100 },
// { label: 'a', value: 3 },
// { label: 'b', value: 1 },
// { label: 'c', value: 2 },
// { label: 'd', value: 1 },
// { label: 'e', value: 5 },
// ]
// */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment