DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

Algorithm 202: Array Merging Without Duplicates in 4 Ways

When given a number of arrays, in how many ways can you merge the arrays without duplicates? I have 4 ways prepared for you!

mergeArrays([1, 2, 3, 3, 3], [1, 4, 5, 3, 2], [1, 4, 8, 9]) 
// [ 1, 2, 3, 4, 5, 8, 9 ]

mergeArrays(["Enugu", "Abia", "Lagos"], ["Lagos", "Sokoto", "Enugu"])
// [ 'Enugu', 'Abia', 'Lagos', 'Sokoto' ]
Enter fullscreen mode Exit fullscreen mode

Prerequisite

To benefit from this article, you need to have basic understanding of javascript's array methods.

Let's do this using:

  • for...of...loop, spread operator, includes(), push(), forEach()
      function mergeArrays(...arrays) {
        let combineArray = [];
        let finalArray = [];

        for (array of arrays) {
          combineArray = [...combineArray, ...array];
        }

        combineArray.forEach(element => {
          if (!finalArray.includes(element)) {
            finalArray.push(element);
          }
        });

        return finalArray;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...of...loop, spread operator, includes(), push(), Array.from()
      function mergeArrays() {
        let givenArray = Array.from(arguments);
        let combineArray = [];
        let finalArray = [];

        for (array of givenArray) {
          combineArray = [...combineArray, ...array];
        }

        for (element of combineArray) {
          if (!finalArray.includes(element)) {
            finalArray.push(element);
          }
        }

        return finalArray;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...of...loop, spread operator, Set()
      function mergeArrays(...arrays) {
        let combineArray = [];
        let finalArray = [];

        for (array of arrays) {
          combineArray = [...combineArray, ...array];
        }

        finalArray = [...new Set([...combineArray])];

        return finalArray;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...of...loop, spread operator, filter()
      function mergeArrays(...arrays) {
        let combineArray = [];
        let finalArray = [];

        for (array of arrays) {
          combineArray = [...combineArray, ...array];
        }

        finalArray = combineArray.filter(
          (element, index) => combineArray.indexOf(element) === index
        );

        return finalArray;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...of...loop, spread operator, includes(), reduce(), Array.from()
      function mergeArrays() {
        let givenArray = Array.from(arguments);
        let combineArray = [];
        let finalArray = [];

        for (array of givenArray) {
          combineArray = [...combineArray, ...array];
        }

        finalArray = combineArray.reduce((acc, cha) => {
          if (acc.includes(cha)) {
            return acc;
          } else {
            return [...acc, cha];
          }
        }, []);

        return finalArray;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 202: Falsy Bouncer in 4 Ways

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (0)