Merging subarrays in JavaScript


This problem says to merge the subarrays with the help of javascript concepts. We will use email data in this issue. Additionally, we will combine emails from people with the same name.

Understanding the problem

For solving any given statement we need to first understand the problem in depth. This problem will be broken down into subproblems. So it will be easy to solve this problem. Consider any important procedures or stages that must be followed in order to solve the issue.

To solve this problem, we must organize the email addresses according to their matching names. This can be done by iterating over the input array while utilizing a map to hold the email addresses for each name.

Algorithm

Step 1 − Declare a function named mergeSubArray taking in an array of elements as an input. This function will group all of the email addresses of the people in an array before returning a new array.

Step 2 − Declare the final emailData array where all the groups of elements present in array input have different combinations. For now declare it with an empty array. The 'Set' will be utilized in this stage to store the email addresses because it eliminates the duplicating data

Step 3 − The input array is looped through for each element. In this phase, the names and emails of the individuals in the current element will be removed. If it is not already one for the current name, create a new batch containing the email and add it to the emailData.

Step 4 − Add each email address to the Set for the name that is now in the emailData.

Step 5 − After looping through the emailData, a new array named showResult will be created and it will contain the sorted email addresses.

Step 6 − So we have sorted the email with their names and put the result in the result array

Step 7 − This is how the grouping and merging the subarrays will take place to provide the results array.

Example

//create an array with name and email
const arr = [  ["Ayaan", "ayaanshri@email.com", "ayaan00@email.com"],
  ["Maahi", "maahibhatt@email.com"],
  ["Ayaan", "ayaansss@email.com", "ayaan_nepal@email.com"],
  ["Maahi", "maahi@email.com"]
];

//define function mergeSubArray
function mergeSubArray(arr) {
  const emailData = {};

  for (let i = 0; i < arr.length; i++) {
    const name = arr[i][0];
    const mail = arr[i].slice(1);

    if (!emailData.hasOwnProperty(name)) {
      emailData[name] = new Set(mail);
    } else {
      mail.forEach(email => emailData[name].add(email));
    }
  }
  const showResult = [];
  for (const [name, mail] of Object.entries(emailData)) {
    showResult.push([name, ...[...mail].sort()]);
  }
  return showResult;
}
console.log(mergeSubArray(arr));

Output

[
    [
      'Ayaan',
      'ayaan00@email.com',
      'ayaan_nepal@email.com',
      'ayaanshri@email.com',
      'ayaansss@email.com'
    ],
    [ 'Maahi', 'maahi@email.com', 'maahibhatt@email.com' ]
]

Time and Space Complexity

The mergeSubArray() function has an O(n log n) time complexity, where n represents the total number of email addresses in the input array. This is because the function sorts the email addresses and uses Sets to check for duplicates, both of which have a worst−case time complexity of O(n log n). In the worst scenario, the loop through the emailData has an O(n) time complexity, where n is the total number of distinct names in the input array.

The function has a space complexity of O(n), where n is the total number of emails in the input array. This is so that each individual name in the input array can have its own entry in a hash table, which is created by the function. Each entry in the hash table contains a Set with up to n email addresses.

Conclusion

We have done this by utilizing a map to store all the email addresses for every name. The total number of email addresses in the input array, denoted by the integer n, determines the time complexity and the space complexity of the ensuing procedure.

Updated on: 23-Aug-2023

427 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements