Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:06:18+00:00 2026-06-08T14:06:18+00:00

I have this code: def input1 = [‘a’,’b’,’e’,’r’,’t’] input2 = [‘v’,’n’,’m’,’y’] ans = []

  • 0

I have this code:

def input1 = ['a','b','e','r','t']
input2 = ['v','n','m','y']
ans = []

def common(def element,def i) {
  if (element == input2[i]) {
    ans << element
    return
  } else {
    common(element,++i)
  }
}  

for (i=0;i<input1.size();i++) {
  common(input1[i],0)
}

which is generating Stack Overflow error. Why is this happening?

Edit:

I’m trying to create my own way of finding common element between two lists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:06:20+00:00Added an answer on June 8, 2026 at 2:06 pm

    You never check if i is greater than the length of input2, and in Groovy, getting beyond the length of a List returns null

    So on the first element, it will keep looping round

    if (element == input2[i]) {
    

    for ever-increasing values of i, calling the common function every time, as it never matches a

    Guessing at what you are trying to do, this can all be re-written as:

    def input1 = ['a','b','e','r','t']
    def input2 = ['v','n','m','y']
    def ans = input1.intersect( input2 )
    

    But it’s hard to be sure what you want, and you dont explicitly say.

    Edit

    One method of deep recursion that avoids Stack Overflows is to use Groovy’s trampoline method.

    def common
    common = { Object element, Collection list ->
      if( list.size() == 0 ) {                     // element not found.  Return null
        null
      }
      else if( list.head() == element ) {          // element found.  Return it
        element
      }
      else {
        common.trampoline( element, list.tail() )  // Trampoline down the list and check again
      }
    }
    common = common.trampoline()
    
    def elements = ['a','b','e','v','r','t'].collect { // For each element in this list
                     common( it, ['v','n','m','y'] )   // Find if it's in our other list
                   }.findAll()                         // And remove the nulls
    
    assert elements == [ 'v' ]
    

    But I’d still use intersect in this case, the above is just to show one of Groovy’s ways you can avoid too-deep recursion…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code: def some_method(): #i need to get some attrs :( return
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have this code: def __init__(self, a, b, c, d...): self.a = a self.b
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have this code: import re def doReplace(toReplace): i = 1 def chapterReplacer(_): result
I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1):
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )
I have a app/helpers/application_helper.rb file with this code: def clippy(text, bgcolor = #ffffff) text.gsub!('',')
I have this simple code in my user_controller.rb file #listing all users def index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.