Finding outliers

#1

Hi , how can i implement finding outlier ?
arr = [1,2,3,4,5,10]
arr << 20
arr<< -5

outputs is = -5,20

#2

Please define “outliers”. Are they simply the smallest and largest values in the array? If so, [1,2,3,4,5,20,-5].minmax #=> [-5, 20].

#3

I read the post. But sorry it wasn’t clear to me.
Anyways, the definition of outlier on google says:

a person or thing situated away or detached from the main body or system.

Whatever, this means. If you are looking for the new items that has appended to the original array (here arr), then you first have to duplicate this and assign that to a variable (and probably freeze it). The solution of my understanding goes something like this:

#!/usr/bin/env ruby
a = 1, 2, 3, 4, 5, 10
b = a.dup

a << 20
a << -5

p a - b         # => [20, -5]

This is not very memory efficient because you just duplicate whatever items wer in the array a.
So here’s another simple and memory friendly solution:

#!/usr/bin/ruby -w
a = 1, 2, 3, 4, 5, 10
b = a.size

a << 20
a << -5
a << 300

p a[b..-1]    # => [20, -5, 300]

Now this is assuming outlier is something that returns the appended elements. If it’s about finding the minimum and maximum elements, see @Cary’s answer…