Check Duplicate Array (i.e. Check if the elements of array are repeated or not)

In this post I want to write how to check if the elements of an array are repeated or not. It is simple task but little bit logical.

class Check
  def check_array(array)
    l = array.length - 1
      for i in (0..l)
        for j in (i+1 .. l)
          if array[i] == array[j]
            return "duplicate"
          end
       end
     end
     return "Not Duplicate"
   end
end
array = [1, 2, 5, 3, 7, 4, 6]
obj = Check.new
puts obj.check_array(array)

One thought on “Check Duplicate Array (i.e. Check if the elements of array are repeated or not)

  1. I am glad that you tried it out Ganesh. Even thought for loop is syntactically right its definitely not the ruby way and there is one more thing I would like to suggest you to give more revealing name to your method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s