Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created June 1, 2021 01:55
Show Gist options
  • Save Shaddyjr/c2e0117291f7c28cdd8c833f7e637674 to your computer and use it in GitHub Desktop.
Save Shaddyjr/c2e0117291f7c28cdd8c833f7e637674 to your computer and use it in GitHub Desktop.
# source: https://www.hackerrank.com/challenges/equality-in-a-array/problem
# video: https://youtu.be/CLIVaqu39wU
def equalizeArray(arr):
# keep dict of counts
counts = {}
# keep highest count item
highest_count_item = arr[0]
# loop through list to take counts
for item in arr: # O(n)
if item not in counts:
counts[item] = 0
counts[item] += 1
# checking if highest count
prev_highest_count = counts[highest_count_item]
if prev_highest_count < counts[item]:
highest_count_item = item
counts.pop(highest_count_item)
return sum(counts.values()) # O(n)
# Total Time Complexity = O(n) + O(n) = O(2n) => O(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment