Last active
August 5, 2019 18:41
-
-
Save bartcis/151cfdc2706389204c8d0b557d8b8c27 to your computer and use it in GitHub Desktop.
Difference between two arrays - solution 1
This file contains hidden or 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
| const arrayOne = [1, 4, 5, 7, 3, 8, 1, 9]; | |
| const arrayTwo = [3, 7, 1, 12, 9, 5, 24, 16]; | |
| function diffArrayBasic(array1, array2) { | |
| // 1 - Create empty string to be returned | |
| let newArray = []; | |
| // 2 - Function that finds unique element in regards to the other array | |
| function uniqueElement(first, second) { | |
| // 3 - Loop through an array | |
| for (let element of first) { | |
| // 4. If a second array doesn't have element from a first | |
| if (second.indexOf(element) === -1) { | |
| // 5. Add unique element to the new array | |
| newArray.push(element); | |
| } | |
| } | |
| } | |
| // 6. Run function twice for both arrays | |
| uniqueElement(array1, array2); | |
| uniqueElement(array2, array1); | |
| // 7. Return final array | |
| return newArray; | |
| } | |
| console.time('Start Algo 1'); | |
| console.log(diffArrayBasic(arrayOne, arrayTwo)); | |
| console.timeEnd('Start Algo 1'); // Start Algo 1: 0.639892578125ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment