user3680799
user3680799

Reputation: 115

Find how many keys have the same value in a hashtable?

In a hashtable(Java), How can I find how many keys have the same value?

lets says I have:

Hashtable<String, int> table = new Hashtable<String, int>();
table.put("a", 1);
table.put("b", 2);
table.put("c", 2);
table.put("d", 2);

in this case the keys: b, c, & d would have the same value. how can I detect that?

Upvotes: 2

Views: 5658

Answers (3)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Map<Integer, Integer> occurenceForValue = new HashMap<Integer, Integer>();
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
Iterator it = table.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    if(!occurenceForValue.containsKey(pairs.getValue())
    {
        occurenceForValue.put(pairs.getValue(), 1);
    }
    else
    {
        occurenceForValue.put(pairs.getValue(), occurenceForValue.get(pairs.getValue()) + 1);
    }


    it.remove(); 
}

Then occurenceForValue will contains, for each value (as key), the number of occurences.

Edit : Note in my code that I corrected your HashTable definition which used int as generic type which is not allowed.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

You can't have a primitive type in a Collection (you need to use the wrapper type). I would recommend you use the diamond operator as well. I would get the keySet and iterate the keys, get each value and add it to a SortedSet (iff the Set didn't contain it already, if it did I would print it). So, I believe you are looking for something like this,

Hashtable<String, Integer> table = new Hashtable<>();
table.put("a", 1);
table.put("b", 2);
table.put("c", 2);
table.put("d", 2);
Set<String> keySet = table.keySet();
SortedSet<Integer> toCount = new TreeSet<>();
for (String key : keySet) {
    Integer val = table.get(key);
    if (!toCount.contains(val)) {
        System.out.printf("Key %s has value %d%n", key, val);
        toCount.add(val);
    } else {
        System.out.printf("Key %s also has value %d%n", key, val);
    }
}

Which outputs

Key b has value 2
Key a has value 1
Key d also has value 2
Key c also has value 2

Upvotes: 1

nem035
nem035

Reputation: 35491

First of all, you have to use reference types (objects) in Hashtable definition. You cannot use a primitive type like int, you have to use Integer.

As far as your problem, you could use a small function like this to count how many times a certain value is in the HashTable:

int countOccurences(Hashtable<String, Integer> table, int value) {
    int count = 0;
    for(String key : table.keySet()) {
        if(table.get(key) == value) {
            count++;
        }
    }
    return count;
}

So if you want to know how many times the value 2 occurs in the table:

Hashtable<String, Integer> table = new Hashtable<String, Integer>();
table.put("a", 1);
table.put("b", 2);
table.put("c", 2);
table.put("d", 2);
System.out.println(countOccurences(table, 2));

This would print 3

Upvotes: 1

Related Questions