Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion algorithms/arrays/top_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ def top_1(arr):
continue

return result


def top_1_v2(arr):
# Once Iteration
ans = []
mostcount = 0
count = {}
for x in arr:
count[x] = count.get(x, 0) + 1
if count[x] > mostcount:
ans = [x]
mostcount = count[x]
elif count[x] == mostcount:
ans.append(x)
return ans
7 changes: 7 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
max_ones_index,
trimmean,
top_1,
top_1_v2,
limit,
n_sum
)
Expand Down Expand Up @@ -338,6 +339,12 @@ def test_top_1(self):
self.assertListEqual(top_1([1 , 1, 2, 2, 3]), [1, 2])
self.assertListEqual(top_1([1, 2, 3, 324, 234, 23, 23, 1, 23, 23]), [23])

class TestTop1V2(unittest.TestCase):

def test_top_1_v2(self):
self.assertListEqual(top_1_v2([1 , 1, 2, 2, 3]), [1, 2])
self.assertListEqual(top_1_v2([1, 2, 3, 324, 234, 23, 23, 1, 23, 23]), [23])


class TestLimit(unittest.TestCase):

Expand Down