-
Notifications
You must be signed in to change notification settings - Fork 340
Bonfire Sorted Union
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
- Difficulty: 2/5
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
Check the assertion tests for examples.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
function unite(arr1, arr2, arr3) {
return arr1;
}
unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);
- The program has to return a new array of unique values from two original arrays in the order they show up. So there is not sorting required, and no duplicates.
- Since you have no idea how many parameters were passed, it would be best to loop through the
arguments
before looping through the arrays.
- I used loops, you can use something else like map, reduce or others if you want.
- You will have to check if the current value is already on the array to be returned for every value.
Solution ahead!
function unite(arr1, arr2, arr3) {
// Creates an empty array to store our final result.
var finalArray = [];
// Loop through the arguments object to truly made the program work with two or more arrays
// instead of 3.
for (var i = 0; i < arguments.length; i++) {
var arrayArguments = arguments[i];
// Loops through the array at hand
for (var j = 0; j < arrayArguments.length; j++) {
var indexValue = arrayArguments[j];
// Checks if the value is already on the final array.
if (finalArray.indexOf(indexValue) < 0) {
finalArray.push(indexValue);
}
}
}
return finalArray;
}
- Check comments in code.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3