Append to a list under a key07.03
The mutation is: if absent, put key → [x]; else values.get(key).append(x). Do not put key → [x] on a later item. That overwrite drops the old list.
get-then-append needs the list to already exist. compute-if-absent / put-if-absent is the helper that creates [] once.
Same skill, three traces. Read all three before you try it.
01second append - given
a = [1, 9, 1], group by value, last1- walk
1already[0]append index
2- result
1→[0,2]ifa=[1,9,1]
02first creates - given
a = [4, 1, 9], first9- walk
9absentput
[2]- result
9→[2]
03empty start - given
a = [], append4under key0- walk
create list
- result
{0:[4]}
append(m, k, x).m is a map from key to list. k is the key. x is the item to append.If
k is absent, put [x]. Else append x onto the existing list. Do not replace the list with [x].a = [1, 9, 1], group by value, last 1 → 1→[0,2] if a=[1,9,1].a = [4, 1, 9], first 9 → 9→[2].a = [], append 4 under key 0 → {0:[4]}.constraintskeys are integers. Values under a key are lists.
- Group Anagrams0/3 skills ready · hash maps · mediumopen
- Group Anagrams0/3 skills ready · hash maps · mediumopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.