#APL(NARS), 20 chars, 40 bytes {(,2)≡∪+/¨{⍵=w}¨w←⍵} Test: f←{(,2)≡∪+/¨{⍵=w}¨w←⍵} f 7,13,9,2,10,2,4,10,7,13,4,9 1 f 77,31,5,31,80,77,5,8,8 0 f 2,4,6,4,4,4 0