Get All Keys in Hash with Same Value

Get all keys in hash with same value

Maybe this?

h.select {|k, v| v == val}.keys.each {|k| @highest_wf_words << [k]}

Or this:

@highest_wf_words.concat(h.select {|k, v| v == val}.keys.map {|k| [k]})

Finding n keys that all hash to same hash value

Given that there are n buckets, with n2 keys, using the pigeonhole principle, we know that one bucket has at least n keys

To figure this out, we're going to need to loop over all the keys

int keys = n * n;
for(int i = 0; i < keys; i++)

Next we way to see which keys collide is to store each key in a list/set for each group of collisions

List<List<Integer>> collisions = new ArrayList<List<Integer>>(n);
for(int i = 0; i < n; i++)
collisions.add(new LinkedList<Integer>());
collisions.get(hash(key)).add(key);

Once we have all the collisions, it's simple to look for a list with at least n collisions

for(List<Integer> collision : collisions)
if(collision.size() >= n)
return collision; //or just print

Putting it all together...

List<Integer> findCollisions(int n)
{
List<List<Integer>> collisions = new ArrayList<List<Integer>>(n);
for(int i = 0; i < n; i++)
collisions.add(new LinkedList<Integer>());
int keys = n * n;

for(int i = 0; i < keys; i++)
collisions.get(hash(i)).add(i);

for(List<Integer> collision : collisions)
if(collision.size() >= n)
return collision;
return null; //this should never happen, due to pigeonholes, but compiler doesn't know
}

How to find a hash key containing a matching value

You could use Enumerable#select:

clients.select{|key, hash| hash["client_id"] == "2180" }
#=> [["orange", {"client_id"=>"2180"}]]

Note that the result will be an array of all the matching values, where each is an array of the key and value.

Hashmap shows same value for all keys

The code

fieldValue.put(..., tmp);

does not clone the list tmp, it just puts into the map as a value the reference to the list.

So finally you have in the map the last content of the list.

You should not recycle the tmp variable, but you should always create a new independent list.

Perl list all keys in hash with identical values

Your example won't work as you want because the -n option puts a while loop around your one-line program, so the hash you declare is created and destoyed for every record in the file. You could get around that by not declaring the hash, and so making it a persistent package variable which will retain all values stored in it.

You can then write push @{ $hash{$F[2]} }, $F[0] but notice that it should be $F[0] etc. and not @F[0], and I have used push to create a list of column 1 values for each column 3 value instead of just a list of one-to-one values relating each column 1 value with its column 3 value.

To clarify, your method produces a hash looking like this, which has to be searched to produce the display that you want.

(
Beth => "Maize",
David => "Apple",
Don => "Corn",
Jared => "Apple",
Mike => "Apple",
Sam => "Apple",
)

while mine creates this, which as you can see is pretty much already in the form you want.

(
Apple => ["Mike", "Jared", "Sam", "David"],
Corn => ["Don"],
Maize => ["Beth"],
)

But I think this problem is a bit too big to be solved with a one-line Perl program. The solution below expects the path to the input file as a command-line parameter, like this

> perl prog.pl colons.csv

but it will default to myfile.csv if no file is specified.

use strict;
use warnings;

our @ARGV = 'myfile.csv' unless @ARGV;

my %data;
while (<>) {
my @fields = split /:/;
push @{ $data{$fields[2]} }, $fields[0];
}

while (my ($k, $v) = each %data) {
next unless @$v > 1;
printf qq{Keys with value "%s": %s\n}, $k, join ', ', @$v;
}

output

Keys with value "Apple": Mike, Jared, Sam, David

Get all keys from ruby hash also where value is array with hashes

def get_keys(object)
if object.is_a? Hash
(object.keys + get_keys(object.values)).flatten.uniq
elsif object.is_a? Array
object.collect{|value| get_keys value}
else
[]
end
end

I think this is quite clear, and you can nest as much as you want.
If the object is not an array or hash, then there are no keys.
If it's an array, it will look for the keys that could be in all the elements
And if it's a hash, it will return its keys, plus the keys of the hashes that could be on the values of the hash. Then you apply flatten to remove possible empty arrays, and uniq to remove duplicate values.

With arrays, it will get the keys that could be inside of the

Finding All The Keys With the Same Value in a Python Dictionary

Try this,

In [26]: [k for k,v in dict1.items() if v == 'y']
Out[26]: ['Bob', 'Jim']

And please don't use dict as a variable name.



Related Topics



Leave a reply



Submit