Skip to content

Commit 3416ebd

Browse files
committed
Unique Tags in DOM tree
1 parent 554908b commit 3416ebd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

challenges/DOM.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
5. [Write a function to find the corresponding node in two identical DOM trees](#Q5)
1212
6. [Write a function to get depth of a given DOM tree](#Q6)
1313
7. [Implement a function to get the root node of a given DOM fragment](#Q7)
14+
8. [Implement a function to get unique tag names in a given DOM tree](#Q8)
1415

1516
---
1617

@@ -227,6 +228,36 @@ function getRootNode (tree) {
227228
###### References
228229
- https://javascript.info/dom-navigation
229230

231+
232+
#### Q8
233+
### Implement a function to get unique tag names in a given DOM tree
234+
235+
```js
236+
/**
237+
* @param {HTMLElement | null} tree
238+
* @return {Array}
239+
*/
240+
function getUniqueTags(root, result = new Set()) {
241+
if(!root) return [];
242+
243+
if(!result.has(root.tagName)) {
244+
result.add(root.tagName);
245+
}
246+
247+
if(root.hasChildNodes()) {
248+
for(let child of root.children) {
249+
getUniqueTags(child, result)
250+
}
251+
}
252+
253+
return [...result];
254+
}
255+
```
256+
257+
###### References
258+
- https://bigfrontend.dev/problem/get-DOM-tags
259+
260+
230261
<br />
231262

232263
[[] Back to top](#home)

0 commit comments

Comments
 (0)