Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Tom Nunn
Tom Nunn
16,333 Points

Find the Odd Number of Integers in a given Array

When given an Array - through a Method. How can I locate the Integer in the given array which appears an odd amount of times?

i.e

array = [1,1,1,1,1,1,10,1,1,1,1]

So in this array 1 appears 10 times (even), where 10 appears once (odd).

What would be the best way to approach this? I was attempting to use the .each method and a block...

def find_odd_integer(seq)
    seq.each do |odd|
    # Solution
end

1 Answer

I'm not sure if you just want the answer or some hints, so I created a gist for the algorithm linked here. If you want to tackle this yourself, try tracking the number of times an integer appears using a hash, then incrementing the value of each in your each loop. Keep in mind that hash#select and hash#keys are your friends here.

Tom Nunn
Tom Nunn
16,333 Points

Cheers Greg! I will have another go later with your hints. Much appreciated.