How do I improve remove duplicate algorithm?

2024/9/21 1:50:06

My interview question was that I need to return the length of an array that removed duplicates but we can leave at most 2 duplicates.

For example, [1, 1, 1, 2, 2, 3] the new array would be [1, 1, 2, 2, 3]. So the new length would be 5. I came up with an algorithm with O(2n) I believe. How can I improve that to be the fastest.

def removeDuplicates(nums):if nums is None:return 0if len(nums) == 0:return 0if len(nums) == 1:return 1new_array = {}for num in nums:new_array[num] = new_array.get(num, 0) + 1new_length = 0for key in new_array:if new_array[key] > 2:new_length = new_length + 2else:new_length = new_length + new_array[key]return new_lengthnew_length = removeDuplicates([1, 1, 1, 2, 2, 3])
assert new_length == 5

My first question would be is my algorithm even correct?

Answer

Your logic is correct however he is a simpler method to reach the goal you had mentioned in your question.

Here is my logic.

myl = [1, 1, 1, 2, 2, 3, 1, 1, 1, 2, 2, 3, 1, 1, 1, 2, 2, 3]newl = []for i in myl:if newl.count(i) != 2:newl.append(i)print newl
[1, 1, 2, 2, 3, 3]

Hope this helps.

https://en.xdnf.cn/q/72433.html

Related Q&A

Looking for values in nested tuple

Say I have:t = ((dog, Dog),(cat, Cat),(fish, Fish), )And I need to check if a value is in the first bit of the nested tuple (ie. the lowercase bits). How can I do this? The capitalised values do not m…

Multiple lines user input in command-line Python application

Is there any easy way to handle multiple lines user input in command-line Python application?I was looking for an answer without any result, because I dont want to:read data from a file (I know, its t…

Performance difference between filling existing numpy array and creating a new one

In iterative algorithms, it is common to use large numpy arrays many times. Frequently the arrays need to be manually "reset" on each iteration. Is there a performance difference between fill…

Set space between boxplots in Python Graphs generated nested box plots with Seaborn?

I am trying to set a space between the boxplots (between the green and orange boxes) created with Python Seaborn modules sns.boxplot(). Please see attached the graph, that the green and orange subplot …

Geocoding using Geopy and Python

I am trying to Geocode a CSV file that contains the name of the location and a parsed out address which includes Address number, Street name, city, zip, country. I want to use GEOPY and ArcGIS Geocodes…

Making Python scripts work with xargs

What would be the process of making my Python scripts work well with xargs? For instance, I would like the following command to work through each line of text file, and execute an arbitrary command:c…

TypeError: expected string or buffer in Google App Engines Python

I want to show the content of an object using the following code:def get(self):url="https://www.googleapis.com/language/translate/v2?key=MY-BILLING-KEY&q=hello&source=en&target=ja&quo…

Returning a row from a CSV, if specified value within the row matches condition

Ahoy, Im writing a Python script to filter some large CSV files.I only want to keep rows which meet my criteria.My input is a CSV file in the following formatLocus Total_Depth Average_Depth_sa…

Python multiprocessing pool: dynamically set number of processes during execution of tasks

We submit large CPU intensive jobs in Python 2.7 (that consist of many independent parallel processes) on our development machine which last for days at a time. The responsiveness of the machine slows …

TypeError: cant escape psycopg2.extensions.Binary to binary

I try to store binary file into postgresql through sqlalchemy and file is uploaded from client. A bit google on the error message brings me to this source file:" wrapped object is not bytes or a…