Created
September 22, 2017 15:47
-
-
Save d7ark/06e358797a96874e303a73bb92b87550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var concatArrays = function (arrays) { | |
let concatedArray = []; | |
arrays.forEach(array => { | |
concatedArray = concatedArray.concat(array); | |
}); | |
return concatedArray; | |
} | |
var isDuplicate = function (a, b) { | |
return a === b; | |
} | |
var findDuplicatesIndexes = function (originalIndex, array) { | |
let duplicatesIndexes = []; | |
for (let i = (originalIndex + 1); i < array.length; i++) { | |
if (isDuplicate(array[originalIndex], array[i])) { | |
duplicatesIndexes.push(i); | |
} | |
} | |
return duplicatesIndexes; | |
} | |
var removeDuplicatedIndexes = function (array, indexes) { | |
if (!indexes.length) { | |
return array; | |
} | |
let localArray = Array.from(array); | |
indexes.reverse(); | |
indexes.forEach(index => localArray.splice(index, 1)); | |
return localArray; | |
} | |
var removeDuplicates = function (array) { | |
let localArray = Array.from(array) | |
for (let i = 0; i < (localArray.length - 1); i++) { | |
let duplicates = findDuplicatesIndexes(i, localArray); | |
localArray = removeDuplicatedIndexes(localArray, duplicates); | |
} | |
return localArray; | |
} | |
function newestLastModified (a, b) { | |
return a.lastModified >= b.lastModified; | |
} | |
module.exports = function (...arrays) { | |
if (arrays == null) { | |
return []; | |
} | |
let joined = concatArrays(arrays); | |
let withoutDuplicates = removeDuplicates(joined); | |
let sortedArrays = withoutDuplicates.sort(newestLastModified); | |
return withoutDuplicates; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment