-2

I am running object detection ANPR which prints some vehicle number labels like (text=MH02MH8767)

MH02MH8767
MH02MH8767
MH02MH67
AP03MN7834
AP0N7834
AP03MN7834

One Number at a time. I want to check duplicacy and print Numbers which occur 2 or 3 times.

like:

MH02MH8767
AP03MN7834

and ignore which is print only one time. All this is dynamic.

How can I achieve this?

text variable containing number plate values.

I have used followings:

list1=str(text)
_size = len(list1) 
repeated = [] 

for i in range(_size): 
    k = i + 1
    for j in range(k, _size): 
        if x[i] == x[j] and x[i] not in repeated: 
             repeated.append(x[i]) 
print("repeated",repeated)

and the other one is:

duplicates=[]

for value in list1:
   if list1.count(value) > 1:
      if value not in duplicates:
         duplicates.append(value)

print("duplicates",duplicates) 

What problem I'm facing is how to store this dynamic (text) variable in the list and how to check duplicate because every time list will be updating.

The output should be:

like:

MH02MH8767
AP03MN7834
tripleee
  • 175,061
  • 34
  • 275
  • 318
Grv
  • 73
  • 1
  • 1
  • 7

2 Answers2

0

I would suggest using a dict where the key would be the number you have and the value the number of times you get them. Then when you get a number, either it's a new one and then you add it to the dict with a value of 1, otherwise you increase it's value by 1 and if the value is greater or equal to 2, you print the duplicate.

Like this:

duplicates = {}
chars = str(text)

for char in chars:
    if char not in duplicates.keys():
        duplicates[char] = 1
    else:
        duplicates[char] += 1
    if duplicates[char] >= 2:
        print('Found duplicate' + char)

Or even more concise

duplicates = {}
chars = str(text)

for char in chars:
    duplicates[char] = duplicates.get(char, 0) + 1
    if duplicates[char] >= 2:
        print('Found duplicate' + char)
bastantoine
  • 582
  • 4
  • 21
0

You can store the elements in the array then use the following code to print the duplicates

arr = [1, 2, 3, 4, 2, 7, 8, 8, 3];
print("Duplicate elements in given array: ");
for i in range(0, len(arr)):
    for j in range(i+1, len(arr)):
        if(arr[i] == arr[j]):
            print(arr[j]);

Result:

Duplicate elements in given array:
2
3 
8
Willy wonka
  • 138
  • 6