-1

I came up with this logic to count the duplicate 1 take input for list length 2 take input of list 3 search in list for the values from zero to last index increment the counter. I am getting error can anyone help to fix it, I know my this not accurate way to do this can someone help me out

n = int(input())
l1=[]
for i in range(n):
    l1.append(input())
print(l1)    
count1=0
count2=0
count3=0
count4=0    
for j in range(n):
       if 1 in l1[0,n-1]:
            count1 =count1+1
       elif 2 in l1(0,n-1):   
            count2=count2+1
       elif 3 in l1(0,n-1):
           count3= count3+1
       elif 4 in l1(0,n-1):
            count4=count4+1          
print(count1)  

input 4 1 1 2 3 4 output should be 2

  • You could use the count method : `len(set([i for i in l if l.count(i)>=2]))` – romain gal Nov 12 '22 at 17:26
  • _"I am getting error can anyone help to fix it"_: Not if you don't tell us what the error is! Please see [ask] and the [question checklist](//meta.stackoverflow.com/q/260648/843953) -- you should [edit] your question to include the _full_ error traceback, which tells us what the error is, and where it happens, so we don't have to guess – Pranav Hosangadi Nov 12 '22 at 17:29
  • `l1.append(input())` input returns a string, so the `l1` list will only contain strings, so `if 1 in l1[0,n-1]` will never be true. – John Gordon Nov 12 '22 at 17:30
  • @JohnGordon thankyou I failed my test today due to this :( , Is there any optimal way to count duplicates in a list – animesh chaudhri Nov 12 '22 at 17:33
  • @PranavHosangadi I am sorry, I don't use stack overflow much I will keep it in mind next time – animesh chaudhri Nov 12 '22 at 17:51
  • Your stated input makes no sense (to me). If the first input value is 4 then the subsequent inputs will be 1, 1, 2 & 3. The last 4 can't be input. Not sure what you thought *l1[0,n-1]* would achieve apart from a TypeError exception. What should the output be if *l1 = [1,1,2,3,3]* ? – DarkKnight Nov 12 '22 at 18:39
  • The problem is that you're checking `if 1 in l1[0,n-1]` where 1 is an integer value. But you collected the user input as a string, so integer 1 can _never_ be in the input list. – John Gordon Nov 12 '22 at 18:44
  • @JohnGordon You would be right if it wasn't for the TypeError – DarkKnight Nov 12 '22 at 18:48
  • @animeshchaudhri The final line in your code is *print(count1)* Does that mean you're only interested in whether (or not) a value of 1 is repeated/duplicated? If that is so, what are the checks for 2, 3 & 4 for (notwithstanding the aforementioned issues)? – DarkKnight Nov 12 '22 at 19:03
  • @Cobra the first input is the length of the list, then values of the list – animesh chaudhri Nov 13 '22 at 15:35

2 Answers2

1

Simply use set() to remove the duplicates from the original list, then take the length of the original list minus the length of the new set:

s = set(l1)
count = len(l1) - len(s)

I don't think this is the optimal way to do it, but it is the shortest and most intuitive way.

thedemons
  • 1,139
  • 2
  • 9
  • 25
  • 1
    Why isn't this the optimal way to do it? Not that the set doesn't need to be converted back to a list. `len(set(l1))` gives you the number of elements in the set – Pranav Hosangadi Nov 12 '22 at 17:28
  • @PranavHosangadi I think `set()` performs multiple memory allocations and copy operations in the heap, doesn't it? – thedemons Nov 12 '22 at 17:32
-2

There is a pre built function in list to count the elements

data = [1,2,3,4,1,4]
print("Count of 1 =", data.count(1))
print("Count of 2 =", data.count(2))
print("Count of 3 =", data.count(3))
print("Count of 4 =", data.count(4))

But if the number of duplicate elements is what is expected then count of all the elements and sort only those greater than 1.

Get all number of elements which are duplicate

data = [1,2,3,4,1,4]
print(len([data.count(x) for x in list(set(data)) if data.count(x)>1]))

sourin_karmakar
  • 389
  • 3
  • 10