--- title: 2022 年資訊科技產業專案設計課程第 2 次作業 tags: INFO2022 --- # 2022 年「[資訊科技產業專案設計](https://hackmd.io/@sysprog/info2022)」第 2 次作業 > 貢獻者: 藍筱裘 Basketball 🍙:interviewer 🏀:interviewee ## [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) ==[模擬面試錄影](https://youtu.be/m91OqfktkiE)== ### 測驗說明與問答 🍙:Given an integer array numbers, I want you to return ==true== if any value appears at least twice in the array, and return ==false== if every element is distinct. ``` [1, 2, 3, 1] => True [1, 2, 3, 5] => False ``` #### REACTO 🏀:Let me repeat and correct me if I'm wrong. Given an integer number array, find if there's any number that is duplicate. If we find the duplicate number, just immediately return true. Otherwise, return false. 🍙:You're right. 🏀:Okay, can you tell me the length of the array? 🍙:The constraint of this problem is that the length of the array is from ==1 to 10000==. 🏀:Are the numbers in the array sorted? 🍙:The array is not sorted. 🏀:Can I type in Python language? 🍙:Ok. Just type in your familiar language. 🏀:Then the bruto solution comes to my mind is sorting the numbers in the array. After sorted, just compare each index of the number and I can easily return True or False. 🍙:Sounds feasible. Just type it down. #### *Naive Solution* 🏀:Sort the array first, and compare the adjacent number in the array. ```python= def findDuplicate(arr): arr.sort() for index in range(0, len(arr - 1)): if arr[index] == arr[index-1]: return True return False ``` 🍙:There's no big problem, but can you solve this problem without using the sorting function? 🏀:I think I can use the ==hash map==. 🍙:Okay, try it. #### Without sorting function 🏀:I can declare a dictionary as the hash map. For every number in the array, if it's already in the dictionary, it means the array contains duplicate number. ```python= def findDuplicate(arr): map = {} for num in arr: if num in map: return True map[num] = True return False ``` 🍙:It'll probably work, thanks for today's interview. 🏀:Thank you. ### 面試過程檢討 #### interviewer - [1:00](https://youtu.be/m91OqfktkiE?t=60) 可再確認陣列中值的範圍 - [2:45](https://youtu.be/m91OqfktkiE?t=165) return False 應在 for 迴圈之外 #### interviewee - [2:45](https://youtu.be/m91OqfktkiE?t=165) 未注意 interviewer 程式碼打錯 - [4:03](https://youtu.be/m91OqfktkiE?t=243) Follow up 的部分可再追問若要找是哪個數字重複或哪個位置重複該如何做?