Finding common elements in two arrays of different size

后端 未结 10 898
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 18:08

I have a problem to find common elements in two arrays and that\'s of different size.

Take , Array A1 of size n and Array A2 o

10条回答
  •  不知归路
    2020-12-14 18:51

    class SortedArr
    
        def findCommon(a,b)
    
          j =0
          i =0
          l1=a.length
          l2=b.length
    
          if(l1 > l2)
                len=l1
          else
                len=l2
          end
    
    
         while i < len
              if a[i].to_i > b[j].to_i
                  j +=1
              elsif a[i].to_i < b[j].to_i
                  i +=1
              else   
                  puts a[i] # OR store it in other ds
                  i +=1
                  j +=1
              end
         end
      end
    end
    
     t = SortedArr.new
     t.findCommon([1,2,3,4,6,9,11,15],[1,2,3,4,5,12,15])
    

提交回复
热议问题