What is wrong in the below gradle task?

I just started learning gradle. Tried to create a simple task as shown below:

["ab","cd","ef"].each { flavor ->
task hi {
    doLast {
        "println ${flavor}"
    }
}
}

while excuting above task ,getting error: Cannot add task ‘hi’ as a task with that name already exists.

Can someone please tell me where i am going wrong?

I’m not sure what you don’t understand, the error message is quite obvious.
You are trying to add a task called hi when there already is one.
Which is caused by you trying to create three tasks called hi while looping over the list elements.