[Kotlin] Simple Traversal
I am updating my solution with the help of @pgmreddy, @binlei and this.\n\nIdea: \n- We traverse over pieces and map the first element of each inner array to th
I am updating my solution with the help of @pgmreddy, @binlei and [this](https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/918408/python-5-lines-hashmap/).\n\n**Idea:** \n- We traverse over `pieces` and map the first element of each inner array to the array itself.\n- We then create another array of same size as `arr` and would fill it with data in next step\n- We now traverse over `arr` and find the element in our map. If we find it, we put the whole array to our result array\n- In the end we match if our `result` array has the same contents as `arr`\n\n\n**Example:**\nLet\'s take an example:\n`arr = [91,4,64,78]` and `pieces = [[78],[4,64],[91]]`\n\nStep 1 would produce a map like this:\n78 -> [78]\n4 -> [4, 64]\n91 -> [91]\n\nStep 2: We create array of size 4: `[0, 0, 0 ,0]`\n\nStep 3: Traversing `arr`:\n- Find 91 in map. Found. `result` -> `[91, 0, 0, 0]`\n- Find 4 in the map. Found. `result` -> `[91, 4, 64, 0]`\n- Find 64 in the map. Not Found. `result` -> `[91, 4, 64, 0]`\n- Find 78 in the map. Found. `result` -> `[91, 4, 64, 78]`\n\nSince `arr == result` content-wise, we return true. To test another case, try to make `[4, 64]` as `[64, 4]` in the above example and check.\n\n```\nfun canFormArray(arr: IntArray, pieces: Array<IntArray>): Boolean {\n        val map = hashMapOf<Int, IntArray>()\n    \n        for (piece in pieces) {\n            map[piece[0]] = piece // Mapping first element to inner array\n        }\n    \n        val result = IntArray(arr.size)\n        var i = 0\n    \n        for (num in arr) {\n            if (map.containsKey(num)) { // Found the element. Now we\'ll add all its inner array\'s elements to result array\n                for (p in map[num]!!) {\n                    result[i++] = p\n                }\n            }\n        }\n    \n        return arr.contentEquals(result)\n    }\n```
['Kotlin']