Skip to content

Commit 5e9e235

Browse files
committed
Add Replace Duplicates In A Keyword List as an elixir til
1 parent 2507908 commit 5e9e235

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ _330 TILs and counting..._
6666
### Elixir
6767

6868
- [Append To A Keyword List](elixir/append-to-a-keyword-list.md)
69+
- [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md)
6970

7071
### Git
7172

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Replace Duplicates In A Keyword List
2+
3+
Use the
4+
[`Keyword.put`](http://elixir-lang.org/docs/stable/elixir/Keyword.html#put/3)
5+
function to replace duplicate key entries in a keyword list.
6+
7+
If there are no duplicate entries, the entry will just be added.
8+
9+
```elixir
10+
Keyword.put([a: 1], :b, 2)
11+
[b: 2, a: 1]
12+
```
13+
14+
If there is a duplicate entry, it will be replaced by the new value.
15+
16+
```elixir
17+
> Keyword.put([b: 1, a: 1], :b, 2)
18+
[b: 2, a: 1]
19+
```
20+
21+
If there are multiple duplicate entries, they will all be replaced.
22+
23+
```elixir
24+
> Keyword.put([b: 3, b: 4, a: 1], :b, 2)
25+
[b: 2, a: 1]
26+
```

0 commit comments

Comments
 (0)