0

using Ruby 2.0

if

array_a = [360,370,360,350,360,360,360]

output : array_a = [360,370,360,350,360,360,360]

if 

array_a = [360,360,360,360]

output : array_a = [360]

If all elements are same then only unique array should come, else same array.

I am trying to implement this logic but not able any hint please ?

2 Answers 2

3

You can use Array#uniq, checking if result array's size is 1:

uniq_array_a = array_a.uniq
output = uniq_array_a.one? ? uniq_array_a : array_a
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of array_a.uniq.count == 1 you can do array_a.uniq.one?
Nice Thanks @BroiSatse and Marek Lipka
I noticed the downvote. How could I improve my answer?
0

You can try array_a.uniq

For example:

a = [10, 20, 30, 10, 20] a.uniq # => [10, 20, 30]

2 Comments

If all elements are same in an array then only i want unique array else same as array whatever is there
Yeah, i got it :-) You should go with the answer of Marek Lipka!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.