Created
October 11, 2021 07:48
-
-
Save sushant-at-nitor/eec80c10b42ae14547d594a0c3c29258 to your computer and use it in GitHub Desktop.
Unique Sorted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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