GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Arrays/find_duplicates.py at master · gridl/coding-problems

Expand file tree

/

find_duplicates.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
81 lines (63 loc) · 2.02 KB
/

find_duplicates.py

Copy path

File metadata and controls

81 lines (63 loc) · 2.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
Find duplicates
Find all duplicates in an array where all elements are positive (>0)
and the biggest element in the array could be equal to the length of array.
Note: solve it in one iteration.
=========================================
Each value has its own position/index in the array,
mark the value on that position as negative when the element is found for the first time.
Time Complexity: O(N)
Space Complexity: O(D) , array (in this case set) to save all duplicates
In the second solution 2 hashsets are used, one to check if already exists element like current and
the other has same functionality as the hashset in the first solution.
* This solution is for all kind of numbers
(not as the previous solution, only for positive numbers or smaller elements than the length of array).
Time Complexity: O(N)
Space Complexity: O(D)
'''
##############
# Solution 1 #
##############
def find_duplicates(arr):
n = len(arr)
duplicates = set()
for i in range(n):
idx = abs(arr[i]) - 1
val = arr[idx]
if val > 0:
# mark element as found for the first time
arr[idx] = -val
else:
# this element is a duplicate
duplicates.add(idx + 1)
return duplicates
##############
# Solution 2 #
##############
def find_duplicates_2(arr):
n = len(arr)
duplicates = set()
elements = set()
for i in range(n):
if arr[i] in duplicates:
# this element is already found as duplicate
continue
if arr[i] in elements:
# a duplicate is found
duplicates.add(arr[i])
elements.remove(arr[i])
else:
# a new number
elements.add(arr[i])
return duplicates
###########
# Testing #
###########
# Test 1
# Correct result => [1]
print(find_duplicates([1, 1, 1, 1]))
print(find_duplicates_2([1, 1, 1, 1]))
# Test 2
# Correct result => [4, 2]
print(find_duplicates([4, 2, 4, 2, 1, 4]))
print(find_duplicates_2([4, 2, 4, 2, 1, 4]))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.