
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript group array - find the sets of numbers that can be traveled to using the edges defined
Consider the following input and output arrays −
const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ];
Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined.
That is, in graph theory terms, find the distinct connected components within such a graph. For instance, in the above arrays, there is no way to travel from 4 to 0 so they are in different groups, but there is a way to travel from 1 to 0 (by way of 3) so they are in the same group." To reiterate the desired output is a grouping of transversable nodes, based on a potentially random input set.
We are required to write a JavaScript function that constructs the desired output from the given input.
Example
const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const groupRange = (arr = []) => { const res = [[]]; let count = 0; const a = [0]; let array = arr.map(el => el.split(':').sort((a, b) => a - b)). sort((a, b) => a[0] - b[0]); array.forEach(el => { if (el[0] > a[a.length - 1]) { res.push(el); a.push(el[1]); count++; } else { res[count] = res[count].concat(el); a[a.length - 1] = el[1]; }; }); return res.map(el => [...new Set(el)].sort((a, b) => a - b)); } console.log(groupRange(input));
Output
And the output in the console will be −
[ [ '0', '1', '3' ], [ '4', '5', '6', '8' ] ]
- Related Questions & Answers
- Program to Find Out the Edges that Disconnect the Graph in Python
- Find possible numbers in array that can sum to a target value JavaScript
- Program to find the length of the longest, that can be made using given letters in Python
- Algorithm for sorting array of numbers into sets in JavaScript
- Python Program to find out the size of the bus that can contain all the friends in the group
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++
- C++ program to find out the number of coordinate pairs that can be made
- C++ program to find out the maximum number of cells that can be illuminated
- How can TensorFlow be used with keras.Model to track the variables defined using sequential model?
- Find the primorial of numbers - JavaScript
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- What should be done with the left/ right edges of the content on overflow with JavaScript?
- Transforming array of numbers to array of alphabets using JavaScript