Challenge: Swap Keys with Values of an Associative Array
Here is a coding challenge to test your knowledge of foreach loop and associative arrays.
We'll cover the following
We know that associative arrays provide a map from keys to values. This mapping is unidirectional: values are accessed by keys but not the other way around.
Problem statement #
Assume that you are given an associative array names
as having strings names as element values. Use this associative array and a foreach
loop to initialize another associative array called values
. The new associative array should contain element values that correspond to the keys of the array names.
Input #
string[int] names = [ 1:"one", 7:"seven", 20:"twenty" ];
Output #
writeln(values["twenty"]); //prints 20
Challenge #
Get hands-on with 1400+ tech skills courses.