
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Algorithm for sorting array of numbers into sets in JavaScript
- Find possible numbers in array that can sum to a target value JavaScript
- Transforming array of numbers to array of alphabets using JavaScript
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- How can TensorFlow be used with keras.Model to track the variables defined using sequential model?
- Count of pairs in an array that have consecutive numbers using JavaScript
- Program to find the length of the longest, that can be made using given letters in Python
- Program to Find Out the Edges that Disconnect the Graph in Python
- Variable defined with a reserved word const can be changed - JavaScript?
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++
- Summing array of string numbers using JavaScript
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- Divide Array in Sets of K Consecutive Numbers in C++
- Group objects inside the nested array JavaScript
- Python Program to find out the size of the bus that can contain all the friends in the group
