Concatenation of Array Solution | Runtime 1ms | Time Complexity = O(N)

Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: The array ans is formed as follows:

  • ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
  • ans = [1,2,1,1,2,1]
class Solution {
    public int[] getConcatenation(int[] nums) {
        
        int[] res = new int[nums.length*2];
        for(int i=0; i<nums.length; i++){
            res[i] = nums[i];    //storing all elements in res[] array 
            res[i+nums.length] = res[i];  // res[] array is twice the size of nums,
        }
        return res;
    }
}
Comments (0)