Tianhe Gao

sorted-union

[Intermediate Algorithm Scripting: Sorted Union | freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union)

我的答案

```js function uniteUnique(arr) { for (let i = 1; i < arguments.length; i++) { arr.push(arguments[i]) } return Array.from(new Set(arr.flat().join('').split(''))) .toString() .split(',') .map((x) => Number(x)) }

console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])) // https://stackoverflow.com/a/35609777/12539782 ```

答案

```js function uniteUnique(arr) { // Creates an empty array to store our final result. const finalArray = []

/ Loop through the arguments object to truly make the program work with two or more arrays / instead of 3. for (let i = 0; i < arguments.length; i++) { const arrayArguments = arguments[i]

// Loops through the array at hand for (let j = 0; j < arrayArguments.length; j++) { let indexValue = arrayArguments[j]

// Checks if the value is already on the final array. if (finalArray.indexOf(indexValue) < 0) { finalArray.push(indexValue) } } }

return finalArray } ```

```js function uniteUnique(arr) { const args = […arguments] const result = [] for (let i = 0; i < args.length; i++) { for (let j = 0; j < args[i].length; j++) { if (!result.includes(args[i][j])) { result.push(args[i][j]) } } } return result } ```

```js function uniteUnique(…arr) { return […new Set(arr.flat())] } ```

```js function uniteUnique() { return […arguments] .flat() .filter((item, ind, arr) > arr.indexOf(item) == ind) } ```


No notes link to this note