From c16cfe65aa1973e48463ed20ad8a0f07e3c7e3f9 Mon Sep 17 00:00:00 2001 From: boafire <47714542+boafire@users.noreply.github.com> Date: Wed, 17 Jul 2019 22:51:47 -0400 Subject: [PATCH] Python solution to firstDuplicate --- challenges/firstDuplicate/firstDuplicate.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 challenges/firstDuplicate/firstDuplicate.py diff --git a/challenges/firstDuplicate/firstDuplicate.py b/challenges/firstDuplicate/firstDuplicate.py new file mode 100644 index 0000000..1b84a21 --- /dev/null +++ b/challenges/firstDuplicate/firstDuplicate.py @@ -0,0 +1,11 @@ +def firstDuplicate(a): + seen = set() + for num in a: + if num in seen: + return num + seen.add(num) + return -1 + +assert firstDuplicate([2, 1, 3, 5, 3, 2]) == 3 +assert firstDuplicate([2, 2]) == 2 +assert firstDuplicate([2, 4, 3, 5, 1]) == -1