Skip to content

Latest commit

 

History

History
160 lines (119 loc) · 3.27 KB

1929-Leetcode.md

File metadata and controls

160 lines (119 loc) · 3.27 KB

© Aryan Mishra

  1. Concatenation of Array

C++

#include <vector>

class Solution {
public:
    std::vector<int> getConcatenation(std::vector<int>& nums) {
        // Creating a vector with double of its length
        std::vector<int> arr(nums.size() * 2);

        // Running a loop for each element in nums vector
        for (int i = 0; i < nums.size(); i++) {

            arr[i] = nums[i];
            arr[i + nums.size()] = nums[i];
        }

        return arr;
    }
};

Java

class Solution {
    public int[] getConcatenation(int[] nums) {
        // Creating an array with double of its length
        int arr[] = new int[nums.length * 2];

        // Running a loop for each element in nums array
        for (int i = 0; i < nums.length; i++) {

            arr[i] = nums[i];
            arr[i + nums.length] = nums[i];
        }

        return arr;
    }
}

Python

class Solution:
    def getConcatenation(self, nums):
        # Creating a list with double of its length
        arr = [0] * (len(nums) * 2)

        # Running a loop for each element in nums list
        for i in range(len(nums)):
            arr[i] = nums[i]
            arr[i + len(nums)] = nums[i]

        return arr

Python3

class Solution:
    def getConcatenation(self, nums):
        # Creating a list with double of its length
        arr = [0] * (len(nums) * 2)

        # Running a loop for each element in nums list
        for i in range(len(nums)):
            arr[i] = nums[i]
            arr[i + len(nums)] = nums[i]

        return arr

C

#include <stdio.h>
#include <stdlib.h>

int* getConcatenation(int* nums, int numsSize, int* returnSize) {
    // Creating an array with double of its length
    int* arr = (int*)malloc(2 * numsSize * sizeof(int));

    // Running a loop for each element in nums array
    for (int i = 0; i < numsSize; i++) {

        arr[i] = nums[i];
        arr[i + numsSize] = nums[i];
    }

    *returnSize = 2 * numsSize;
    return arr;
}

C#

public class Solution {
    public int[] GetConcatenation(int[] nums) {
        // Creating an array with double of its length
        int[] arr = new int[nums.Length * 2];

        // Running a loop for each element in nums array
        for (int i = 0; i < nums.Length; i++) {

            arr[i] = nums[i];
            arr[i + nums.Length] = nums[i];
        }

        return arr;
    }
}

JavaScript

class Solution {
    getConcatenation(nums) {
        // Creating an array with double of its length
        const arr = new Array(nums.length * 2);

        // Running a loop for each element in nums array
        for (let i = 0; i < nums.length; i++) {

            arr[i] = nums[i];
            arr[i + nums.length] = nums[i];
        }

        return arr;
    }
}

TypeScript

class Solution {
    getConcatenation(nums: number[]): number[] {
        // Creating an array with double of its length
        const arr: number[] = new Array(nums.length * 2);

        // Running a loop for each element in nums array
        for (let i = 0; i < nums.length; i++) {

            arr[i] = nums[i];
            arr[i + nums.length] = nums[i];
        }

        return arr;
    }
}