I am trying to find the duplicate values in an array of strings between 1 to 1000000.
However, with the code I have, I get the output as all the entries that are doubled.
So for instance, if I have [1,2,3,4,3,4], it gives me the output of 3 4 3 4 instead of 3 4.
Here is my code:
array = [gets]
if array.uniq.length == array.length
puts "array does not contain duplicates"
else
puts "array does contain duplicates"
print array.select{ |x| array.count(x) > 1}
end
Also, every time I test my code, I have to define the array as array = [1,2,3,4,5,3,5]. The puts works but it does not print when I use array [gets].
Can someone help me how to fix these two problems?
a.select{ |x| a.count(x) > 1}.uniqtry thisgetsreturns a string delimited with a newline. You can't input an array like that. You need something likearray = gets.chomp.split.map(&:to_i)and expect the user to enter, say,1 2 3 4 5, followed by return.gets, rather than simply being given an arbitrary arrayarr? If you must usegets, you can only get a string, so you will have to break it up into separate elements and convert that to an array. Of course, the user will have to follow your instructions on how to format the string for conversion to an array.