Square Each Array Value
Problem statement
Return an array containing the square of each input value, preserving order and repeated entries. Do not sort the result.
Follow-up: also describe a dictionary mapping each distinct input value to its square. The judged return value remains the ordered array; the first solution shows the dictionary construction.
Function
squareValues(values: int[]) → int[]Examples
Example 1
values = [1,2,3,4,5]return = [1,4,9,16,25]The values 1 through 5 have squares 1, 4, 9, 16 and 25.
Example 2
values = [-3,2,-3]return = [9,4,9]Squaring preserves the input order and both occurrences of -3.
Constraints
0 <= values.length <= 2000.-1000 <= values[i] <= 1000.