Skip to content

Instantly share code, notes, and snippets.

@sushant-at-nitor
Created October 11, 2021 07:48
Show Gist options
  • Save sushant-at-nitor/eec80c10b42ae14547d594a0c3c29258 to your computer and use it in GitHub Desktop.
Save sushant-at-nitor/eec80c10b42ae14547d594a0c3c29258 to your computer and use it in GitHub Desktop.
Unique Sorted
interface Tree {
[index: string]: number | Tree;
}
const getUniqueSortedNumbers = (tree: Tree, items: number[] = []): number[] => {
Object.keys(tree).map((key: string) =>
typeof tree[key] === 'object'
? getUniqueSortedNumbers(tree[key] as Tree, items)
: items.indexOf(tree[key] as number) <= 0 && items.push(tree[key] as number)
)
return items.sort((a, b ) => a < b ? -1 : 1);
}
const numbers = getUniqueSortedNumbers({
a: {
x: 3,
y: {
d: 2,
e: 2,
f: {
g: 4
},
z: 8
}
}
});
console.log(numbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment