Ho un array Ruby che contiene elementi duplicati.
array = [1,2,2,1,4,4,5,6,7,8,5,6]
Come posso rimuovere tutti gli elementi duplicati da questo array mantenendo tutti gli elementi univoci senza usare for-loops e iterazione?
Ho un array Ruby che contiene elementi duplicati.
array = [1,2,2,1,4,4,5,6,7,8,5,6]
Come posso rimuovere tutti gli elementi duplicati da questo array mantenendo tutti gli elementi univoci senza usare for-loops e iterazione?
Risposte:
array = array.uniq
uniq
rimuove tutti gli elementi duplicati e conserva tutti gli elementi univoci dell'array.
Questa è una delle tante bellezze della lingua Ruby.
[{how: "are"}, {u:"doing"}, {how: "are"}].uniq => [{:how=>"are"}, {:u=>"doing"}]
.uniq!
fa il lavoro sull'oggetto stesso
È possibile restituire l'intersezione.
a = [1,1,2,3]
a & a
Questo eliminerà anche i duplicati.
a | a
(unione) farebbe lo stesso trucco.
Puoi rimuovere gli elementi duplicati con il metodo uniq:
array.uniq # => [1, 2, 4, 5, 6, 7, 8]
Ciò che potrebbe anche essere utile sapere è che uniq
richiede un blocco, quindi se hai un array di chiavi:
["bucket1:file1", "bucket2:file1", "bucket3:file2", "bucket4:file2"]
e vuoi sapere quali sono i file univoci, puoi scoprirlo con:
a.uniq { |f| f[/\d+$/] }.map { |p| p.split(':').last }
uniq
a tale array senza un blocco restituisce lo stesso valore di quello con il blocco.
Solo un'altra alternativa se qualcuno se ne frega.
È inoltre possibile utilizzare il to_set
metodo di una matrice che converte la matrice in una serie e, per definizione, gli elementi della serie sono unici.
[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]
to_set
assegnerà 4 oggetti, mentre uniq
alloca uno.
Se qualcuno stava cercando un modo per rimuovere tutte le istanze di valori ripetuti, vedere " Come posso estrarre in modo efficiente elementi ripetuti in un array Ruby? ".
a = [1, 2, 2, 3]
counts = Hash.new(0)
a.each { |v| counts[v] += 1 }
p counts.select { |v, count| count == 1 }.keys # [1, 3]
a = [1, 2, 2, 3] a.find_all { |x| a.count(x) == 1 } # [1, 3]
Giusto per fornire alcune informazioni:
require 'fruity'
require 'set'
array = [1,2,2,1,4,4,5,6,7,8,5,6] * 1_000
def mithun_sasidharan(ary)
ary.uniq
end
def jaredsmith(ary)
ary & ary
end
def lri(ary)
counts = Hash.new(0)
ary.each { |v| counts[v] += 1 }
counts.select { |v, count| count == 1 }.keys
end
def finks(ary)
ary.to_set
end
def santosh_mohanty(ary)
result = ary.reject.with_index do |ele,index|
res = (ary[index+1] ^ ele)
res == 0
end
end
SHORT_ARRAY = [1,1,2,2,3,1]
mithun_sasidharan(SHORT_ARRAY) # => [1, 2, 3]
jaredsmith(SHORT_ARRAY) # => [1, 2, 3]
lri(SHORT_ARRAY) # => [3]
finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]
puts 'Ruby v%s' % RUBY_VERSION
compare do
_mithun_sasidharan { mithun_sasidharan(array) }
_jaredsmith { jaredsmith(array) }
_lri { lri(array) }
_finks { finks(array) }
_santosh_mohanty { santosh_mohanty(array) }
end
Che, quando eseguito, si traduce in:
# >> Ruby v2.7.1
# >> Running each test 16 times. Test will take about 2 seconds.
# >> _mithun_sasidharan is faster than _jaredsmith by 2x ± 0.1
# >> _jaredsmith is faster than _santosh_mohanty by 4x ± 0.1 (results differ: [1, 2, 4, 5, 6, 7, 8] vs [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, ...
# >> _santosh_mohanty is similar to _lri (results differ: [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, ...
# >> _lri is similar to _finks (results differ: [] vs #<Set: {1, 2, 4, 5, 6, 7, 8}>)
Nota: questi hanno restituito risultati negativi:
lri(SHORT_ARRAY) # => [3]
finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]
Prova a utilizzare l'operatore XOR, senza utilizzare le funzioni integrate:
a = [3,2,3,2,3,5,6,7].sort!
result = a.reject.with_index do |ele,index|
res = (a[index+1] ^ ele)
res == 0
end
print result
Con funzioni integrate:
a = [3,2,3,2,3,5,6,7]
a.uniq
.sort!
anche una funzione integrata?