Thursday, April 30, 2015

intersection of n collections

Increment count of each element of collection only once as it could occur 1+ times in it.

Tests:
{ 1, 1 }, { 1, 2 } => { 1 }
{ 1, 2 }, { 1, 1 } => { 1 }
{ 1 }, { 2 } => {}
{ 1 }, { 1, 2 }, { 1, 1, 2 } => { 1 }
{ 1, 1 }, { 1, 1 } => { 1 }
intersection(c1, c1, ... cn):
    countmap = (cols)
    intersection = (countmap, n)
    return intersection

(countmap, n):
    intersection = new
    for x in countmap.keys():
        if countmap[x] == n:
            intersection.add(x)
    return intersection

(cols):
    countmap = new
    for col in cols:
        set unique = new
        for x in col:
            if !unique.has(x):
                unique.add(x)
                if countmap.has(x):
                    countmap[x]++
                else:
                    countmap[x] = 1
    return countmap

No comments:

Post a Comment