Advertisement
gruntfutuk

find_first_dup

Jul 14th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def find_1st_dup(array):
  2.     found = {}
  3.     for index, item in enumerate(array, start = 1):
  4.         if item in found: return found[item]
  5.         found[item] = index
  6.     return -1
  7.  
  8. def find_1st_dup_value(array):
  9.     found = set()
  10.     for item in array:
  11.         if item in found: return item
  12.         found.add(item)
  13.     return -1
  14.  
  15. tests = ([20, 10, 30, 50, 30, 20],
  16.          [2, 4, 3, 5, 1],
  17.          [5, 3, 4, 3, 6, 5],
  18.          [400, 299, 170, 70, 40, 500, 60, 99, 299, 76])
  19.      
  20. for test in tests:
  21.     print(f'index: {test} is {find_1st_dup(test)}')
  22.     print(f'value: {test} is {find_1st_dup_value(test)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement