Removing duplicates from an array(unsorted) – How can I improve the efficiency of this code /u/not_dr_jaishankar Python Education

Hi all, I was attempting to solve a problem where we remove all the duplicates from an unsorted array. This problem was asked in one of the array tutorials. The catch here is I cannot use any other data structures like sets, dictionary since they haven’t been introduced yet. I have come up with a solution, but I do not think it is very efficient. Please help me on how can I improve the code efficiency.

arr = [1,1,2,3,4,2,2,3,4,5,5,3,7,8,7] new_arr = [] for num in arr: notPresent = True for i in range(len(new_arr)): if num == new_arr[i]: notPresent = False break if notPresent: new_arr.append(num) print(new_arr) 

submitted by /u/not_dr_jaishankar
[link] [comments]

​r/learnpython Hi all, I was attempting to solve a problem where we remove all the duplicates from an unsorted array. This problem was asked in one of the array tutorials. The catch here is I cannot use any other data structures like sets, dictionary since they haven’t been introduced yet. I have come up with a solution, but I do not think it is very efficient. Please help me on how can I improve the code efficiency. arr = [1,1,2,3,4,2,2,3,4,5,5,3,7,8,7] new_arr = [] for num in arr: notPresent = True for i in range(len(new_arr)): if num == new_arr[i]: notPresent = False break if notPresent: new_arr.append(num) print(new_arr) submitted by /u/not_dr_jaishankar [link] [comments] 

Hi all, I was attempting to solve a problem where we remove all the duplicates from an unsorted array. This problem was asked in one of the array tutorials. The catch here is I cannot use any other data structures like sets, dictionary since they haven’t been introduced yet. I have come up with a solution, but I do not think it is very efficient. Please help me on how can I improve the code efficiency.

arr = [1,1,2,3,4,2,2,3,4,5,5,3,7,8,7] new_arr = [] for num in arr: notPresent = True for i in range(len(new_arr)): if num == new_arr[i]: notPresent = False break if notPresent: new_arr.append(num) print(new_arr) 

submitted by /u/not_dr_jaishankar
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *