यहां डुप्लिकेट खोजने के दो और तरीके हैं।
एक सेट का उपयोग करें
require 'set'
def find_a_dup_using_set(arr)
s = Set.new
arr.find { |e| !s.add?(e) }
end
find_a_dup_using_set arr
#=> "hello"
सभी डुप्लिकेट की एक सरणी को वापस करने के selectस्थान पर उपयोग करें find।
उपयोग Array#difference
class Array
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
end
end
def find_a_dup_using_difference(arr)
arr.difference(arr.uniq).first
end
find_a_dup_using_difference arr
#=> "hello"
.firstसभी डुप्लिकेट की एक सरणी वापस करने के लिए ड्रॉप करें।
nilयदि कोई डुप्लिकेट नहीं हैं, तो दोनों विधियाँ वापस आती हैं।
मैंने प्रस्ताव दिया किArray#difference रूबी कोर में जोड़ा जाए। अधिक जानकारी यहाँ मेरे उत्तर में है ।
बेंचमार्क
सुझाए गए तरीकों की तुलना करते हैं। सबसे पहले, हमें परीक्षण के लिए एक सरणी की आवश्यकता है:
CAPS = ('AAA'..'ZZZ').to_a.first(10_000)
def test_array(nelements, ndups)
arr = CAPS[0, nelements-ndups]
arr = arr.concat(arr[0,ndups]).shuffle
end
और विभिन्न परीक्षण सरणियों के लिए मानदंड चलाने की एक विधि:
require 'fruity'
def benchmark(nelements, ndups)
arr = test_array nelements, ndups
puts "\n#{ndups} duplicates\n"
compare(
Naveed: -> {arr.detect{|e| arr.count(e) > 1}},
Sergio: -> {(arr.inject(Hash.new(0)) {|h,e| h[e] += 1; h}.find {|k,v| v > 1} ||
[nil]).first },
Ryan: -> {(arr.group_by{|e| e}.find {|k,v| v.size > 1} ||
[nil]).first},
Chris: -> {arr.detect {|e| arr.rindex(e) != arr.index(e)} },
Cary_set: -> {find_a_dup_using_set(arr)},
Cary_diff: -> {find_a_dup_using_difference(arr)}
)
end
मैंने @ JjP के उत्तर को शामिल नहीं किया क्योंकि केवल एक डुप्लिकेट वापस करना है, और जब उसका उत्तर यह करने के लिए संशोधित किया जाता है कि यह @ नवेद के पहले वाले उत्तर के समान है। न ही मैंने @ मारिन के उत्तर को शामिल किया, जिसने @ नवेद के उत्तर से पहले पोस्ट किया, केवल एक के बजाय सभी डुप्लिकेट लौटाए (एक मामूली बिंदु लेकिन दोनों का मूल्यांकन करने वाला कोई बिंदु नहीं है, क्योंकि वे एक ही नकल करते समय समान हैं)।
मैंने अन्य उत्तरों को भी संशोधित किया है जो सभी डुप्लिकेट्स को वापस लौटाते हैं जो केवल पहले वाले को मिला है, लेकिन इसका प्रदर्शन पर अनिवार्य रूप से कोई प्रभाव नहीं होना चाहिए, क्योंकि उन्होंने एक का चयन करने से पहले सभी डुप्लिकेट की गणना की थी।
प्रत्येक बेंचमार्क के परिणाम सबसे तेज़ से सबसे धीमे सूचीबद्ध हैं:
पहले मान लें कि सरणी में 100 तत्व हैं:
benchmark(100, 0)
0 duplicates
Running each test 64 times. Test will take about 2 seconds.
Cary_set is similar to Cary_diff
Cary_diff is similar to Ryan
Ryan is similar to Sergio
Sergio is faster than Chris by 4x ± 1.0
Chris is faster than Naveed by 2x ± 1.0
benchmark(100, 1)
1 duplicates
Running each test 128 times. Test will take about 2 seconds.
Cary_set is similar to Cary_diff
Cary_diff is faster than Ryan by 2x ± 1.0
Ryan is similar to Sergio
Sergio is faster than Chris by 2x ± 1.0
Chris is faster than Naveed by 2x ± 1.0
benchmark(100, 10)
10 duplicates
Running each test 1024 times. Test will take about 3 seconds.
Chris is faster than Naveed by 2x ± 1.0
Naveed is faster than Cary_diff by 2x ± 1.0 (results differ: AAC vs AAF)
Cary_diff is similar to Cary_set
Cary_set is faster than Sergio by 3x ± 1.0 (results differ: AAF vs AAC)
Sergio is similar to Ryan
अब 10,000 तत्वों के साथ एक सरणी पर विचार करें:
benchmark(10000, 0)
0 duplicates
Running each test once. Test will take about 4 minutes.
Ryan is similar to Sergio
Sergio is similar to Cary_set
Cary_set is similar to Cary_diff
Cary_diff is faster than Chris by 400x ± 100.0
Chris is faster than Naveed by 3x ± 0.1
benchmark(10000, 1)
1 duplicates
Running each test once. Test will take about 1 second.
Cary_set is similar to Cary_diff
Cary_diff is similar to Sergio
Sergio is similar to Ryan
Ryan is faster than Chris by 2x ± 1.0
Chris is faster than Naveed by 2x ± 1.0
benchmark(10000, 10)
10 duplicates
Running each test once. Test will take about 11 seconds.
Cary_set is similar to Cary_diff
Cary_diff is faster than Sergio by 3x ± 1.0 (results differ: AAE vs AAA)
Sergio is similar to Ryan
Ryan is faster than Chris by 20x ± 10.0
Chris is faster than Naveed by 3x ± 1.0
benchmark(10000, 100)
100 duplicates
Cary_set is similar to Cary_diff
Cary_diff is faster than Sergio by 11x ± 10.0 (results differ: ADG vs ACL)
Sergio is similar to Ryan
Ryan is similar to Chris
Chris is faster than Naveed by 3x ± 1.0
ध्यान दें कि find_a_dup_using_difference(arr)अगर बहुत अधिक कुशल होगाArray#difference C में लागू किया गया था, तो यह होगा, अगर यह रूबी कोर में जोड़ा जाता है तो मामला होगा।
निष्कर्ष
जवाब में से कई उचित हैं लेकिन सेट का उपयोग करना सबसे अच्छा विकल्प है । यह मध्यम-कठिन मामलों में सबसे तेज है, संयुक्त सबसे कठिन और केवल कम्प्यूटेशनल रूप से तुच्छ मामलों में - जब आपकी पसंद वैसे भी कोई फर्क नहीं पड़ेगा - क्या इसे पीटा जा सकता है।
एक बहुत विशेष मामला जिसमें आप क्रिस के समाधान को चुन सकते हैं, यदि आप हजारों छोटे सरणियों को अलग-अलग डी-डुप्लिकेट करने के लिए विधि का उपयोग करना चाहते हैं और आमतौर पर 10 से कम आइटमों में डुप्लिकेट खोजने की अपेक्षा करते हैं। यह थोड़ा तेज होगा। क्योंकि यह सेट बनाने के छोटे अतिरिक्त ओवरहेड से बचा जाता है।
arr == arr.uniqयह देखने के लिए एक आसान और सुरुचिपूर्ण तरीका होगा किarrक्या डुप्लिकेट हैं, हालांकि, यह प्रदान नहीं करता है जो डुप्लिकेट थे।