Created
September 18, 2018 05:00
-
-
Save woodRock/869b08e2a722468289be92f3dd318673 to your computer and use it in GitHub Desktop.
Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer. For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def non_duplicate_in_set set | |
freq = set.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash } | |
freq.keys.each { |k| return k if freq[k] < 3 } | |
end | |
set = [6, 1, 3, 3, 3, 6, 6] | |
puts non_duplicate_in_set(set) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment