programing

루비의 배열에서 중복 요소 제거

easyjava 2023. 5. 9. 23:12
반응형

루비의 배열에서 중복 요소 제거

중복 요소가 포함된 루비 배열을 가지고 있습니다.

array = [1,2,2,1,4,4,5,6,7,8,5,6]

for-loops 및 반복을 사용하지 않고 모든 고유 요소를 유지하면서 이 어레이에서 중복 요소를 모두 제거하려면 어떻게 해야 합니까?

array = array.uniq

uniq 중복 요소를 모두 제거하고 배열의 고유한 요소를 모두 유지합니다.

이것은 루비 언어의 많은 아름다움 중 하나입니다.

교차로를 반환할 수 있습니다.

a = [1,1,2,3]
a & a

중복 항목도 삭제됩니다.

중복 요소는 고유한 방법으로 제거할 수 있습니다.

array.uniq  # => [1, 2, 4, 5, 6, 7, 8]

또한 알아두면 유용할 수 있는 것은uniq는 블록을 사용하므로 키 배열이 있는 경우 다음을 수행합니다.

["bucket1:file1", "bucket2:file1", "bucket3:file2", "bucket4:file2"]

고유한 파일이 무엇인지 알고 싶다면 다음을 통해 확인할 수 있습니다.

a.uniq { |f| f[/\d+$/] }.map { |p| p.split(':').last }

반복되는 값의 모든 인스턴스를 제거하는 방법을 찾고 있는 경우 "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]

신경쓰는 사람이 있다면 다른 대안일 뿐입니다.

사용할 수도 있습니다.to_set배열을 집합으로 변환하는 배열의 메서드이며, 정의에 따라 집합 요소는 고유합니다.

[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]

저에게 가장 간단한 방법은 다음과 같습니다.

array = [1, 2, 2, 3]

Array#to_set

array.to_set.to_a

# [1, 2, 3]

Array#uniq

array.uniq

# [1, 2, 3]

몇 가지 통찰력을 제공하기 위해:

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

이를 실행하면 다음과 같은 결과가 발생합니다.

# >> 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}>)

참고: 잘못된 결과가 반환되었습니다.

  • lri(SHORT_ARRAY) # => [3]
  • finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
  • santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]

내장 기능을 사용하지 않고 XOR 연산자를 사용해 보십시오.

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

내장 기능 포함:

a = [3,2,3,2,3,5,6,7]

a.uniq

언급URL : https://stackoverflow.com/questions/8365721/remove-duplicate-elements-from-array-in-ruby

반응형