I have a sorted list, and I would like to count the number of occurrence of each number without using count() function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I have a sorted list, and I would like to count the number of occurrence of each number without using count() function



I have a sorted list, and I would like to count the number of occurrences of each number without using the count() function.


count()


sameItem = 0
startPosition = 1

sortedList = [13, 15, 15, 17, 18, 18, 18, 18, 19, 20, 20, 20, 20, 21, 22, 22, 22, 22, 23, 23, 23, 24, 24, 26, 26, 26, 27, 27, 27, 28]

for i in range(1, len(sortedList)):

item1 = sortedList[i - 1]
item2 = sortedList[i]
countItems = 1
sameItem = countItems

if item1 == item2:
startPosition = i
while (sortedList[i - 1] == sortedList[startPosition]):
sameItem += 1
startPosition += 1

else:
sameItem = countItems

print(str(item1) + " appears " + str(sameItem) + " times")





What is your question?
– bstrauch24
Aug 6 at 13:54





One way to do it is to make an counter array and make it the size of the largest number. so in your case int countArray[29]; Now you can do something like countArray[13]++ to count the number of times 13 appears. Then sum up the values of the countArray.
– Katianie
Aug 6 at 13:54





This might help you. Count occurrence in sorted array
– Nisarg
Aug 6 at 13:55





2 Answers
2



You can use itertools.groupby:


itertools.groupby


from itertools import groupby
for k, g in groupby(sortedList):
print('%s appears %d times' % (k, len(list(g))))



Or if you prefer not to use any library functions:


count = 1
for i, n in enumerate(sortedList):
if i == len(sortedList) - 1 or n != sortedList[i + 1]:
print('%s appears %d times' % (n, count))
count = 1
else:
count += 1



Or if you prefer not to use any function at all (well actually print is a function too but I don't think you can do without it):


print


last = None
for n in sortedList:
if n != last:
if last is not None:
print('%s appears %d times' % (last, count))
last = n
count = 1
else:
count += 1
print('%s appears %d times' % (last, count))



All of the above output:


13 appears 1 times
15 appears 2 times
17 appears 1 times
18 appears 4 times
19 appears 1 times
20 appears 4 times
21 appears 1 times
22 appears 4 times
23 appears 3 times
24 appears 2 times
26 appears 3 times
27 appears 3 times
28 appears 1 times





But i would like to convert it to mips...so i wont be using any libraries... is there a way to do it with my current code (with a little bit of tweaking)?
– IronMan 101
Aug 6 at 15:21





I see. I've updated my answer with a solution that uses no library functions then.
– blhsing
Aug 6 at 15:40





is there a way to do it without enumerate? cuz i'm trying to write an algorithm without any functions used..
– IronMan 101
Aug 6 at 16:29





I've updated my answer with a solution that uses no function at all then.
– blhsing
Aug 7 at 1:13





i have added my current solution below..they are mostly correct, except for the last few output
– IronMan 101
Aug 7 at 4:47



i = 1



check = False



sameItem = 0



while (i < len(sortedList)):


if sortedList[i-1] == sortedList[i]:
check = True
j = i

while (sortedList[i-1] == sortedList[j]) and (check == True):
sameItem += 1
j += 1

if (sortedList[i-1] != sortedList[j]):
check = False
i = j

print(str(sortedList[i-1]) + " appears " + str(sameItem) + " times")

else:
sameItem = 1
print(str(sortedList[i-1]) + " appears " + str(sameItem) + " times")

i += 1





Your solution is unnecessarily complicated to be frank. There's no need to have maintain two indexes when you're always comparing only the adjacent items. The check flag also doesn't make any sense.
– blhsing
Aug 7 at 5:02


check






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard