RemoveDuplicates

Run Settings
LanguagePython
Language Version
Run Command
# Basic Python Syntax and Data Structures # Question: Write a Python function that takes a list of integers and returns a list with only the unique elements (i.e., remove duplicates). # Example: Input: [1, 2, 2, 3, 4, 4, 5], Output: [1, 2, 3, 4, 5]. # inputs: list of integers # outputs: list of integers (no dupicates) def removeDuplicatesFromIntegerArray(int_array): array_noDuplicates = [] duplicatesBuffer = dict() for element in int_array: element_str = str(element) if element_str not in duplicatesBuffer: duplicatesBuffer[element_str] = True array_noDuplicates.append(element) # print(element) return array_noDuplicates def main(): Array1 = [2, 1, 2, 2, 2, 3, 4, 5] print(removeDuplicatesFromIntegerArray(Array1)) if __name__ == "__main__": main()
Editor Settings
Theme
Key bindings
Full width
Lines