Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
|
||||
p ary.uniq # => [1, 2, "redundant", [1, 2, 3]]
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
class Array
|
||||
# used Hash
|
||||
def uniq1
|
||||
each_with_object({}) {|elem, h| h[elem] = true}.keys
|
||||
end
|
||||
# sort (requires comparable)
|
||||
def uniq2
|
||||
sorted = sort
|
||||
pre = sorted.first
|
||||
sorted.each_with_object([pre]){|elem, uniq| uniq << (pre = elem) if elem != pre}
|
||||
end
|
||||
# go through the list
|
||||
def uniq3
|
||||
each_with_object([]) {|elem, uniq| uniq << elem unless uniq.include?(elem)}
|
||||
end
|
||||
end
|
||||
|
||||
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
|
||||
p ary.uniq1 #=> [1, 2, "redundant", [1, 2, 3]]
|
||||
p ary.uniq2 rescue nil # Error (not comparable)
|
||||
p ary.uniq3 #=> [1, 2, "redundant", [1, 2, 3]]
|
||||
|
||||
ary = [1,2,3,7,6,5,2,3,4,5,6,1,1,1]
|
||||
p ary.uniq1 #=> [1, 2, 3, 7, 6, 5, 4]
|
||||
p ary.uniq2 #=> [1, 2, 3, 4, 5, 6, 7]
|
||||
p ary.uniq3 #=> [1, 2, 3, 7, 6, 5, 4]
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
def unique(array)
|
||||
pure = Array.new
|
||||
for i in array
|
||||
flag = false
|
||||
for j in pure
|
||||
flag = true if j==i
|
||||
end
|
||||
pure << i unless flag
|
||||
end
|
||||
return pure
|
||||
end
|
||||
|
||||
unique ["hi","hey","hello","hi","hey","heyo"] # => ["hi", "hey", "hello", "heyo"]
|
||||
unique [1,2,3,4,1,2,3,5,1,2,3,4,5] # => [1,2,3,4,5]
|
||||
Loading…
Add table
Add a link
Reference in a new issue