mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 09:01:44 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
66 lines
1.4 KiB
Markdown
66 lines
1.4 KiB
Markdown
---
|
|
id: 6821ecb5237de8297eaee7a2
|
|
title: "Python Challenge 20: Array Duplicates"
|
|
challengeType: 29
|
|
dashedName: python-challenge-20
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an array of integers, return an array of integers that appear more than once in the initial array, in the same order that they appear in the initial array. If no values appear more than once, return an empty array.
|
|
|
|
# --hints--
|
|
|
|
`find_duplicates([1, 2, 3, 4, 5])` should return `[]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_duplicates([1, 2, 3, 4, 5]), [])`)
|
|
}})
|
|
```
|
|
|
|
`find_duplicates([1, 2, 3, 4, 1, 2])` should return `[1, 2]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_duplicates([1, 2, 3, 4, 1, 2]), [1, 2])`)
|
|
}})
|
|
```
|
|
|
|
`find_duplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4])` should return `[-6, 0, 2, 4, 5, 23]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(find_duplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4]), [-6, 0, 2, 4, 5, 23])`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def find_duplicates(arr):
|
|
|
|
return arr
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def find_duplicates(arr):
|
|
seen = set()
|
|
duplicates = set()
|
|
|
|
for num in arr:
|
|
if num in seen:
|
|
duplicates.add(num)
|
|
else:
|
|
seen.add(num)
|
|
|
|
return sorted(duplicates)
|
|
```
|