score:13

Accepted answer

if you mean only the values that occur more than once, you can do group the key/value pairs by value, filter out the groups with only one item and select the common group key (original value that appears multiple times) from what remains:

var multiples = dictionary.groupby(p => p.value)
                          .where(g => g.count() > 1)
                          .select(g => g.key);

if you want all pairs of keys/values where the values occur more than once, this variation will do it:

var multiples = dictionary.groupby(p => p.value)
                          .where(g => g.count() > 1)
                          .selectmany(g => g);

the only difference is in the last step, where after casting out groups with just one value the contents of all remaining groups are "unwrapped" into a single sequence of key/value pairs.

in the latter case, you can turn the results back into a dictionary (essentially filtering out values that appear only once) by following up with

.todictionary(p => p.key, p => p.value)

the last example, in query form:

var multiples = from pair in dictionary
                group pair by pair.value into grp
                where grp.count() > 1
                from pair in grp select pair;

score:0

dictionary<int,string> d = new dictionary<int,string>();
d.add(1, "entries");
d.add(2, "images");
d.add(3, "views");
d.add(4, "images");
d.add(5, "results");
d.add(6, "images");
d.add(7, "entries");

d.groupby(x => x.value)
 .where(x=>x.count()>1)
 .selectmany(x => x)
 .todictionary
 (
     x => x.key,
     x=>x.value
 );

score:0

with these extension methods you have a (little bit) faster and general solution (works on all ienumarable sources):

public static class extensions
{
    public static ienumerable<tsource> duplicates<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> selector)
    {
        var grouped = source.groupby(selector);
        var morethen1 = grouped.where(i => i.ismultiple());
        return morethen1.selectmany(i => i);
    }

    public static bool ismultiple<t>(this ienumerable<t> source)
    {
        var enumerator = source.getenumerator();
        return enumerator.movenext() && enumerator.movenext();
    }
}

fill the source:

// create the source
var dictionary = new dictionary<int, string>
    {
        {1, "entries"},
        {2, "images"},
        {3, "views"},
        {4, "images"},
        {5, "results"},
        {6, "images"},
        {7, "entries"}
    };

here are 3 options (each option is improved):

// more or less the same solution as other answers
var multiples1 = dictionary.groupby(p => p.value)
                            .where(g => g.count() > 1)
                            .selectmany(g => g);

// a little bit faster because ismultiple does not enumerate all values 
// (count() iterates to the end, which is not needed in this case).
var multiples2 = dictionary.groupby(p => p.value)
                            .where(g => g.ismultiple())
                            .selectmany(g => g);

// easy to read
var multiples3 = dictionary.duplicates(p => p.value);

score:2

var res = dict.groupby(x => x.value).where(y => y.count() > 1);

if you want to get a dictionary:

var res = dict.groupby(x => x.value)
              .where(y => y.count() > 1)
              .selectmany(z => z)
              .todictionary(k => k.key, k => k.value);

Related Query

More Query from same tag