You are given a method that takes in an array of unsorted integers and you must return the deduped array. The interviewer runs it as three progressively tighter variants, using each one to gauge your understanding of time and space complexity.
Implement a function dedupe_array that takes an array of integers as input and returns a new array with duplicates removed. The function will be tested with three different constraints:
Input: [3, 1, 2, 3, 4, 2, 5]
Output: [3, 1, 2, 4, 5]
Input: [3, 1, 2, 3, 4, 2, 5]
Output: [3, 1, 2, 4, 5]
Input: [3, 1, 2, 3, 4, 2, 5]
Output: [3, 1, 2, 4, 5]
python def dedupe_array(arr): seen = set() result = [] for num in arr: if num not in seen: seen.add(num) result.append(num) return result
python def dedupe_array(arr): seen = set() result = [] for num in arr: if num not in seen: seen.add(num) result.append(num) return result
`python def dedupe_array(arr): if not arr: return arr
write_index = 1
for read_index in range(1, len(arr)):
if arr[read_index] != arr[read_index - 1]:
arr[write_index] = arr[read_index]
write_index += 1
return arr[:write_index]
`
The above solution and problem statement are based on the provided excerpt and common interview question patterns.