Advertisement
Iam_Sandeep

Untitled

Nov 24th, 2021
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. class Solution:    
  2.     #Function to return the count of number of elements in union of two arrays.
  3.     def doUnion(self,a,m,b,n):
  4.         a.sort()
  5.         b.sort()
  6.         i=j=0
  7.         t=[]
  8.         while i<m and j<n:
  9.             if a[i]<b[j]:
  10.                 if not t or (t and a[i]!=t[-1]) :
  11.                     t.append(a[i])
  12.                 i=i+1
  13.             elif a[i]>b[j]:
  14.                 if  not t or (t and b[j]!=t[-1]) :
  15.                     t.append(b[j])
  16.                 j=j+1
  17.             else:
  18.                 if not t or (t and a[i]!=t[-1]) :
  19.                     t.append(a[i])
  20.                 i,j=i+1,j+1
  21.         while i<m:
  22.             if  not t or (t and a[i]!=t[-1]) :
  23.                 t.append(a[i])
  24.                 i=i+1
  25.         while j<n:
  26.             if  not t or (t and b[j]!=t[-1]) :
  27.                 t.append(b[j])
  28.                 j=j+1
  29.         return len(t)
  30.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement