Common list elements: Difference between revisions

added RPL
(added RPL)
 
(7 intermediate revisions by 5 users not shown)
Line 205:
{{out}}
<pre>[ 3 9 6 ]</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find common elements of lists #
 
PRIO COMMON = 1;
# returns the common elements of a and b #
OP COMMON = ( []INT a, b )[]INT:
IF INT a len = ( UPB a - LWB a ) + 1;
INT b len = ( UPB b - LWB b ) + 1;
a len < 1 OR b len < 1
THEN # one or both lists is/are empty #
[]INT()
ELIF a len < b len
THEN # both lists are non-empty, b is shorter #
b COMMON a
ELSE # both lists are non-empty, a is at most as long as b #
[ 1 : b len ]INT result;
[ LWB a : UPB a ]BOOL used;
FOR i FROM LWB a TO UPB a DO used[ i ] := FALSE OD;
INT r pos := 0;
FOR b pos FROM LWB b TO UPB b DO
BOOL found := FALSE;
FOR a pos FROM LWB a TO UPB a WHILE NOT found DO
IF NOT used[ a pos ] THEN
IF ( found := a[ a pos ] = b[ b pos ] ) THEN
result[ r pos +:= 1 ] := b[ b pos ];
used[ a pos ] := TRUE
FI
FI
OD
OD;
result[ : r pos ]
FI # COMMON # ;
# returns the common elements in the lists in nums #
OP COMMON = ( [][]INT nums )[]INT:
IF 1 UPB nums < 1 LWB nums THEN # no lists #
[]INT()
ELIF 1 UPB nums = 1 LWB nums THEN # only one list #
nums[ LWB nums ]
ELSE # two or more lists #
FLEX[ 1 : 0 ]INT result;
result := nums[ LWB nums ] COMMON nums[ LWB nums + 1 ];
FOR i FROM LWB nums + 2 TO UPB nums DO
result := result COMMON nums[ i ]
OD;
result
FI # COMMON # ;
 
print( ( COMMON [][]INT( ( 2, 5, 1, 3, 8, 9, 4, 6 )
, ( 3, 5, 6, 2, 9, 8, 4 )
, ( 1, 3, 7, 6, 9 )
)
)
)
END
</syntaxhighlight>
{{out}}
<pre>
+3 +6 +9
</pre>
 
=={{header|APL}}==
Line 332 ⟶ 393:
[2,5,1,3,8,9,4,6],[3,5,6,2,9,8,4],[1,3,7,6,9] : 3 6 9
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">(∊/⊣)´ ⟨2,5,1,3,8,9,4,6⟩‿⟨3,5,6,2,9,8,4⟩‿⟨1,3,7,6,9⟩</syntaxhighlight>
{{out}}
<pre>⟨ 3 9 6 ⟩</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">contains = proc [T: type] (a: array[T], v: T) returns (bool)
Line 584 ⟶ 651:
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
nums[][] = [ [ 2 5 1 3 8 9 4 6 ] [ 3 5 6 2 9 8 4 ] [ 1 3 7 6 9 ] ]
#
found = 1
for e in nums[1][]
for l = 2 to len nums[][]
found = 0
for x in nums[l][]
if e = x
found = 1
break 1
.
.
if found = 0
break 1
.
.
if found = 1
r[] &= e
.
.
print r[]
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 858 ⟶ 950:
{{out}}
<pre>{3, 6, 9}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
common_elems(lst):=block(map(setify,lst),apply(intersection,%%),listify(%%))$
nums:[[2,5,1,3,8,9,4,6],[3,5,6,2,9,8,4],[1,3,7,6,9]]$
common_elems(nums);
</syntaxhighlight>
{{out}}
<pre>
[3,6,9]
</pre>
 
=={{header|Nim}}==
Line 1,078 ⟶ 1,181:
<pre>
common list elements are: [3,6,9]
</pre>
 
=={{header|RPL}}==
Can handle duplicates.
{{works with|HP|48}}
« SWAP LIST→ → n
« 'n' DECR DUP 3 + ROLL - 2 +
ROLL DROP n →LIST
» » '<span style="color:blue">POPL</span>' STO <span style="color:grey">@ ''( {list} idx_item_to_remove → {list} )''</span>
« '''IF''' OVER SIZE OVER SIZE < '''THEN''' SWAP '''END'''
0 → a j
« { } SWAP
'''WHILE''' 'j' INCR a SIZE ≤ '''REPEAT'''
a j GET
'''IF''' DUP2 POS '''THEN'''
LASTARG ROT SWAP <span style="color:blue">POPL</span>
ROT ROT + SWAP
'''ELSE''' DROP '''END'''
'''END''' DROP
» » '<span style="color:blue">INTER</span>' STO
 
{ 2 5 1 3 8 9 4 6 } { 3 5 6 2 9 8 4 } { 1 3 7 6 9 } <span style="color:blue">INTER</span> <span style="color:blue">INTER</span>
{ 2 2 1 3 8 9 4 6 } { 3 5 6 2 2 2 4 } { 2 3 7 6 2 } <span style="color:blue">INTER</span> <span style="color:blue">INTER</span>
{{out}}
<pre>
2: { 3 6 9 }
1: { 3 6 2 2 }
</pre>
 
Line 1,194 ⟶ 1,325:
{{libheader|Wren-seq}}
As we're dealing here with lists rather than sets, some guidance is needed on how to deal with duplicates in each list in the general case. A drastic solution would be to remove all duplicates from the result. Instead, the following matches duplicates - so if List A contains 2 'a's and List B contains 3 'a's, there would be 2 'a's in the result.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
 
var common2 = Fn.new { |l1, l2|
Line 1,244 ⟶ 1,375:
<br>
Since the above was written, we can also now offer a library based solution.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
 
var lls = [
1,150

edits