246,253 views
22 votes
22 votes
Write a function that removes duplicates from an array. For example, if remove_ duplicates is called with an array containing 1 4 9 16 9 7 4 9 11 then the array is changed to 1 4 9 16 7 11 Your function should have a reference parameter for the array size that is updated when removing the duplicates.

User Jake Spencer
by
2.6k points

1 Answer

10 votes
10 votes

Answer:

Step-by-step explanation:

The following code is written in Python. It takes in an array of numbers as a parameter and loops through it. It checks to see if each element is already inside the new_arr array and if not then it adds it. Finally, it prints out the new_arr, prints the new_arr size and returns new_arr to the user which contains all the numbers from the original array without the duplicates.

def removeDuplicates(arr):

new_arr = []

for element in arr:

if element not in new_arr:

new_arr.append(element)

print(new_arr)

print("New Array Size: " + str(len(new_arr)))

return new_arr

test_arr = [1, 4, 9, 16, 9, 7, 4, 9, 11]

removeDuplicates(test_arr)

Write a function that removes duplicates from an array. For example, if remove_ duplicates-example-1
User TheInnerLight
by
2.7k points