3

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?

11
  • 5
    a.select{ |x| a.count(x) > 1}.uniq try this Commented Jun 3, 2015 at 6:06
  • May be duplicate of this Commented Jun 3, 2015 at 6:19
  • gets returns a string delimited with a newline. You can't input an array like that. You need something like array = gets.chomp.split.map(&:to_i) and expect the user to enter, say, 1 2 3 4 5, followed by return. Commented Jun 3, 2015 at 6:21
  • So how do I do do it? It asks me for an array between 1 to 1000000. How do I submit my answer then? Commented Jun 3, 2015 at 6:22
  • Are you sure you have to use gets, rather than simply being given an arbitrary array arr? If you must use gets, 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. Commented Jun 3, 2015 at 6:29

5 Answers 5

5

How I wish we had a built-in method Array#difference:

class Array
  def difference(other)
    h = other.tally
    reject { |e| h[e] > 0 && h[e] -= 1 }
  end
end

though @user123's answer is more straightforward. (Array#difference is probably the more efficient of the two, as it avoids the repeated invocations of count.) See my answer here for a description of the method and links to its use.

In a nutshell, it differs from Array#- as illustrated in the following example:

a = [1,2,3,4,3,2,4,2]
b = [2,3,4,4,4]

a - b          #=> [1]
a.difference b #=> [1, 3, 2, 2]

For the present problem, if:

arr = [1,2,3,4,3,4]

the duplicate elements are given by:

arr.difference(arr.uniq).uniq
  #=> [3, 4]
Sign up to request clarification or add additional context in comments.

Comments

4

For your first problem, you need to uniq function like

array.select{ |x| array.count(x) > 1}.uniq

For your second problem, when you receive a value using array = [gets] it would receive your entire sequence of array numbers as a single string, so everything would be stored in a[0] like ["1, 2 3 4\n"].

Comments

1
puts "Enter array"
array = gets.chomp.split(",").map(&:to_i)
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}.uniq
end

copy this code in ruby file and try to run using

ruby file_name.rb

7 Comments

A small thing: chomp! is not needed.
it is needed to remove '\n' which comes in last element
chomp! is not needed, chomp is enough, because you are storing the returned array.
@ShailiParikh have you checked it
Neither chomp nor chomp! are needed. Try it.
|
0

Coming to your 'gets' problem,

When you are doing a gets, your are basically getting a string as an input but not an array.

2.2.0 :001 > array = [gets]
1,2,1,4,1,2,3
 => ["1,2,1,4,1,2,3\n"]

See the above example, how the ruby interpreter took all your elements as a single string and put it in an array as a single array element. So you need to explicitly convert the input to an array with comma as a delimiter. The below will address both your questions.

array = gets.chomp
array = array.split(',').map(&:to_i)
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}.uniq!
end

Comments

0

Classical method

   array = [1,2,2,3,4,5,5,5,56,7,8,2,1,3]
   duplicates = []

    0.upto(array.size-1) do |i|
    
       (i+1).upto (array.size-1) do |j|
          if(array[i] == array [j])
            duplicates.push(array[i])
          end
       end
    end

    puts "duplicates=> #{duplicates}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.