Concatenation Of Array

Solution For Concatenation Of Array

Problem Statement:

Given two arrays of integers nums1 and nums2, concatenate them into a single array.

Example 1:
Input: nums1 = [1,2,3], nums2 = [4,5,6,7] Output: [1,2,3,4,5,6,7] Explanation: Concatenate nums1 and nums2 into [1,2,3,4,5,6,7].

Example 2:
Input: nums1 = [1], nums2 = [] Output: [1] Explanation: Concatenate nums1 and nums2 into [1].

Solution:

To solve this problem, we can simply create a new array and copy all elements of the given arrays into the new array in sequence. The time complexity of this approach is O(n), where n is the total number of elements in both the arrays.

Algorithm:

  1. Create an empty array arr3 to store the concatenation of the two arrays.

  2. Traverse through the first array nums1 and add each element to the arr3 using the push() method.

  3. Traverse through the second array nums2 and add each element to the arr3 using the push() method.

  4. Return the final array arr3 as the output.

Javascript Implementation:

“`
/*
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
/
var getConcatenation = function(nums1, nums2) {
let arr3 = []; // Creating an empty array to store the concatenation of nums1 and nums2

// Adding all the elements of nums1 to arr3
for(let i=0; i<nums1.length; i++){
    arr3.push(nums1[i]);
}

// Adding all the elements of nums2 to arr3
for(let i=0; i<nums2.length; i++){
    arr3.push(nums2[i]);
}

return arr3; // Returning the concatenated array

};
“`

Time Complexity: O(n), where n is the total number of elements in both the arrays.

Space Complexity: O(n), as we are creating a new array to store the concatenated elements of the given arrays.

Step by Step Implementation For Concatenation Of Array

class Solution {
    public List concatenatedWords(String[] words) {
        List result = new ArrayList<>();
        Set set = new HashSet<>();
        for(String word : words) {
            set.add(word);
        }
        for(String word : words) {
            if(word.length() == 0) {
                continue;
            }
            boolean flag = true;
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < word.length(); i++) {
                sb.append(word.charAt(i));
                if(set.contains(sb.toString())) {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                result.add(word);
            }
        }
        return result;
    }
}
def concatenate(arr1, arr2): 

# initialize an empty list 
result = [] 

# traverse in the 1st list 
for ele in arr1: 
	result.append(ele) 

# now extend 2nd list in the result 
for ele in arr2: 
	result.append(ele) 

return result
// given an array of arrays, concatenate them into a single array
function concat(arrays) {
  // your code here
}
class Solution {
public:
    string concatenation(vector& arr) {
        string res = "";
        for (string& s : arr) {
            res += s;
        }
        return res;
    }
};
public class Solution {
    public IList ConcatenatedWords(string[] words) {
        // create a list to store all the concatenated words
        List concatenatedWords = new List();
        
        // create a set to store all the words
        HashSet wordSet = new HashSet();
        
        // add all the words to the set
        foreach(string word in words) {
            wordSet.Add(word);
        }
        
        // loop through all the words
        foreach(string word in words) {
            // if the word has already been added to the list of concatenated words, continue
            if(concatenatedWords.Contains(word)) {
                continue;
            }
            
            // create a list of all the suffixes of the current word
            List suffixes = new List();
            
            for(int i = 1; i < word.Length; i++) {
                suffixes.Add(word.Substring(i));
            }
            
            // create a list of all the prefixes of the current word
            List prefixes = new List();
            
            for(int i = 0; i < word.Length - 1; i++) {
                prefixes.Add(word.Substring(0, i + 1));
            }
            
            // check if any of the suffixes are present in the set
            bool suffixPresent = false;
            
            foreach(string suffix in suffixes) {
                if(wordSet.Contains(suffix)) {
                    suffixPresent = true;
                    break;
                }
            }
            
            // if no suffix is present, continue
            if(!suffixPresent) {
                continue;
            }
            
            // check if any of the prefixes are present in the set
            bool prefixPresent = false;
            
            foreach(string prefix in prefixes) {
                if(wordSet.Contains(prefix)) {
                    prefixPresent = true;
                    break;
                }
            }
            
            // if no prefix is present, continue
            if(!prefixPresent) {
                continue;
            }
            
            // if the current word is a concatenated word, add it to the list
            concatenatedWords.Add(word);
        }
        
        return concatenatedWords;
    }
}


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]