Common list elements: Difference between revisions
added RPL
Alextretyak (talk | contribs) (Added 11l) |
(added RPL) |
||
(26 intermediate revisions by 18 users not shown) | |||
Line 13:
=={{header|11l}}==
<
V r = Set(nums[0])
L(num) nums[1..]
Line 19:
R r
print(cle([[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]]))</
{{out}}
<pre>
Set([3, 6, 9])
</pre>
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
DEFINE PTR="CARD"
PROC PrintArray(BYTE ARRAY a BYTE len)
BYTE i
Print(" [")
IF len>0 THEN
FOR i=0 TO len-1
DO
PrintB(a(i))
IF i<len-1 THEN Put(' ) FI
OD
FI
PrintE("]")
RETURN
BYTE FUNC Contains(BYTE ARRAY a BYTE len,value)
BYTE i,count
count=0
IF len>0 THEN
FOR i=0 TO len-1
DO
IF a(i)=value THEN count==+1 FI
OD
FI
RETURN (count)
PROC CommonListElements(PTR ARRAY arrays
BYTE ARRAY lengths BYTE count
BYTE ARRAY res BYTE POINTER resLen)
BYTE ARRAY a
BYTE i,j,len,value,cnt,maxcnt
resLen^=0
IF count=0 THEN RETURN FI
FOR i=0 TO count-1
DO
IF lengths(i)=0 THEN RETURN FI
OD
a=arrays(0) len=lengths(0)
IF count=1 THEN
MoveBlock(res,a,len) RETURN
FI
FOR i=0 TO len-1
DO
value=a(i)
IF Contains(res,resLen^,value)=0 THEN
maxcnt=Contains(a,len,value)
FOR j=1 TO count-1
DO
cnt=Contains(arrays(j),lengths(j),value)
IF cnt<maxcnt THEN maxcnt=cnt FI
OD
IF maxcnt>0 THEN
FOR j=1 TO maxcnt
DO
res(resLen^)=value resLen^==+1
OD
FI
FI
OD
SortB(res,resLen^,0)
RETURN
PROC Test(PTR ARRAY arrays BYTE ARRAY lengths BYTE count)
BYTE ARRAY res(100)
BYTE len,i
CommonListElements(arrays,lengths,count,res,@len)
PrintE("Input:")
FOR i=0 TO count-1
DO
PrintArray(arrays(i),lengths(i))
OD
PrintE("Intersection:")
PrintArray(res,len) PutE()
RETURN
PROC Main()
PTR ARRAY arrays(3)
BYTE ARRAY
lengths(3)=[8 7 5],
a1(8)=[2 5 1 3 8 9 4 6],
a2(7)=[3 5 6 2 9 8 4],
a3(5)=[1 3 7 6 9],
a4(8)=[2 2 1 3 8 9 4 6],
a5(7)=[3 5 6 2 2 2 4],
a6(5)=[2 3 7 6 2],
a7(5)=[0 1 7 8 9]
BYTE len
Put(125) PutE() ;clear the screen
arrays(0)=a1 arrays(1)=a2 arrays(2)=a3
Test(arrays,lengths,3)
arrays(0)=a4 arrays(1)=a5 arrays(2)=a6
Test(arrays,lengths,3)
arrays(2)=a7
Test(arrays,lengths,3)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Common_list_elements.png Screenshot from Atari 8-bit computer]
<pre>
Input:
[2 5 1 3 8 9 4 6]
[3 5 6 2 9 8 4]
[1 3 7 6 9]
Intersection:
[3 6 9]
Input:
[2 2 1 3 8 9 4 6]
[3 5 6 2 2 2 4]
[2 3 7 6 2]
Intersection:
[2 2 3 6]
Input:
[2 2 1 3 8 9 4 6]
[3 5 6 2 2 2 4]
[0 1 7 8 9]
Intersection:
[]
</pre>
=={{header|Ada}}==
<
with Ada.Containers.Vectors;
Line 67 ⟶ 202:
R := Common_Elements (R, C);
Put (R);
end Common;</
{{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}}==
APL has the built-in intersection function <code>∩</code>
<
3 9 6 </
=={{header|AppleScript}}==
===AppleScriptObjC===
<
use framework "Foundation"
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
Line 94 ⟶ 290:
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}})
tell sorter to sort(commonElements, 1, -1)
return commonElements</
{{output}}
<syntaxhighlight lang
===Core language only===
The requirement for AppleScript 2.3.1 is only for the 'use' command which loads the "Insertion Sort" script. If the sort's instead loaded with the older 'load script' command or copied into the code, this will work on systems as far back as Mac OS X 10.5 (Leopard) or earlier. Same output as above.
<
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
Line 127 ⟶ 323:
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}})
tell sorter to sort(commonElements, 1, -1)
return commonElements</
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">commonElements: function [subsets][
if zero? size subsets -> return []
if 1 = size subsets -> return first subsets
result: first subsets
loop slice subsets 1 dec size subsets 'subset [
result: intersection result subset
]
return result
]
print commonElements [
[2 5 1 3 8 9 4 6]
[3 5 6 2 9 8 4]
[1 3 7 6 9]
]</syntaxhighlight>
{{out}}
<pre>3 6 9</pre>
=={{header|AutoHotkey}}==
<
counter := [], output := []
for i, num in nums
Line 138 ⟶ 358:
return output
}
</syntaxhighlight>
Examples:<
output := Common_list_elements(nums)
return</
{{out}}
<pre>[3, 6, 9]</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COMMON_LIST_ELEMENTS.AWK
BEGIN {
Line 168 ⟶ 388:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
[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)
where T has equal: proctype (T,T) returns (bool)
for i: T in array[T]$elements(a) do
if i=v then return(true) end
end
return(false)
end contains
common = proc [T: type] (lists: ssT) returns (sT)
where T has equal: proctype (T,T) returns (bool)
sT = sequence[T]
aT = array[T]
ssT = sequence[sequence[T]]
cur: aT := sT$s2a(ssT$bottom(lists))
for i: int in int$from_to(2, ssT$size(lists)) do
next: aT := aT$[]
for e: T in sT$elements(lists[i]) do
if contains[T](cur, e) then
aT$addh(next,e)
end
end
cur := next
end
return(sT$a2s(cur))
end common
start_up = proc ()
si = sequence[int]
ssi = sequence[sequence[int]]
nums: ssi := ssi$[
si$[2,5,1,3,8,9,4,6],
si$[3,5,6,2,9,8,4],
si$[1,3,7,6,9]
]
po: stream := stream$primary_output()
for i: int in si$elements(common[int](nums)) do
stream$puts(po, int$unparse(i) || " ")
end
end start_up</syntaxhighlight>
{{out}}
<pre>3 6 9</pre>
=={{header|Excel}}==
===LAMBDA===
Line 181 ⟶ 453:
{{Works with|Office 365 Betas 2021}}
<
=LAMBDA(xs,
LAMBDA(ys,
Line 205 ⟶ 477:
xs
)
)</
and also assuming the following generic bindings in Name Manager:
<
=LAMBDA(x,
LAMBDA(xs,
Line 248 ⟶ 520:
)
)
)</
{{Out}}
Line 336 ⟶ 608:
|
|}
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
const Set1: set of byte = [2,5,1,3,8,9,4,6];
const Set2: set of byte = [3,5,6,2,9,8,4];
const Set3: set of byte = [1,3,7,6,9];
procedure CommonListElements(Memo: TMemo);
{Using Delphi "sets" to find common elements}
var I,Start,Stop: integer;
var Common: set of byte;
var S: string;
begin
{Uses "*" intersection set operator to}
{ find items common to all three sets}
Common:=Set1 * Set2 * Set3;
Memo.Lines.Add('Common Elements in');
Memo.Lines.Add(' [2,5,1,3,8,9,4,6]');
Memo.Lines.Add(' [3,5,6,2,9,8,4]');
Memo.Lines.Add(' [1,3,7,6,9]: ');
S:='';
{Display the common items}
for I:=0 to 9 do
if I in Common then S:=S+IntToStr(I)+',';
Memo.Lines.Add(S);
end;
</syntaxhighlight>
{{out}}
<pre>
Common Elements in
[2,5,1,3,8,9,4,6]
[3,5,6,2,9,8,4]
[1,3,7,6,9]:
3,6,9,
Elapsed Time: 5.260 ms.
</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#}}==
Of course it is possible to use sets but I thought the idea was not to?
<
// Common list elements. Nigel Galloway: February 25th., 2021
let nums=[|[2;5;1;3;8;9;4;6];[3;5;6;2;9;8;4];[1;3;7;6;9]|]
printfn "%A" (nums|>Array.reduce(fun n g->n@g)|>List.distinct|>List.filter(fun n->nums|>Array.forall(fun g->List.contains n g)));;
</syntaxhighlight>
{{out}}
<pre>
Line 352 ⟶ 692:
Note: in older versions of Factor, <code>intersect-all</code> was called <code>intersection</code>.
{{works with|Factor|0.99 2021-02-05}}
<
{ { 2 5 1 3 8 9 4 6 } { 3 5 6 2 9 8 4 } { 1 3 7 6 9 } } intersect-all .</
{{out}}
<pre>
{ 3 6 9 }
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as integer nums(1 to 3, 1 to 8) = {{2,5,1,3,8,9,4,6}, {3,5,6,2,9,8,4}, {1,3,7,6,9} }
redim as integer outp(0)
dim as integer i, j
dim as boolean found
function is_in( s() as integer, n as integer, r as integer ) as boolean
for i as uinteger = 1 to ubound(s,2)
if s(r,i)=n then return true
next i
return false
end function
for i = 1 to 8
found = true
for j = 2 to 3
if not is_in( nums(), nums(1,i), j ) then found = false
next j
if found then
redim preserve as integer outp(1 to 1+ubound(outp))
outp(ubound(outp)) = nums(1,i)
end if
next i
for i = 1 to ubound(outp)
print outp(i);" ";
next i</syntaxhighlight>
{{out}}<pre>3 9 6</pre>
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 423 ⟶ 792:
fmt.Println()
}
}</
{{out}}
Line 433 ⟶ 802:
[3 6 2 2]
</pre>
=={{header|Haskell}}==
<
task :: Ord a => [[a]] -> [a]
Line 440 ⟶ 810:
task xs = Set.toAscList . foldl1 Set.intersection . map Set.fromList $ xs
main = print $ task [[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</
{{out}}
<pre>[3,6,9]</pre>
=={{header|J}}==
<syntaxhighlight lang="j"> 2 5 1 3 8 9 4 6([-.-.)3 5 6 2 9 8 4([-.-.)1 3 7 6 9
3 9 6</syntaxhighlight>
Or,
<syntaxhighlight lang="j"> ;([-.-.)&.>/2 5 1 3 8 9 4 6;3 5 6 2 9 8 4;1 3 7 6 9
3 9 6</syntaxhighlight>
=={{header|jq}}==
Line 451 ⟶ 831:
on the arrays whose intersection is sought. The helper function, `ios`,
might be independently useful and so is defined as a top-level filter.
<
# then [a,b] | ios will emit the stream of elements in the set-intersection of a and b.
def ios:
Line 471 ⟶ 851:
elif any(.[]; length == 0) then []
else sort_by(length) | go
end;</
<
[[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</
{{out}}
<pre>
Line 479 ⟶ 859:
</pre>
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>{x^x^y}/(2 5 1 3 8 9 4 6;1 3 7 6 9;3 5 6 2 9 8 4)
3 9 6</syntaxhighlight>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
# Find the common list elements in an integer array nums[][]
# # Variables:
#
typeset -a nums=( (2 5 1 3 8 9 4 6) (3 5 6 2 9 8 4) (1 3 7 6 9) )
# # Functions:
#
# # Function _flatten(arr[][] arr[]) - flatten input matrix
#
function _flatten {
typeset _inarr ; nameref _inarr="$1"
typeset _outarr ; nameref _outarr="$2"
typeset _i ; integer _i
_oldIFS=$IFS ; IFS=\|
for ((_i=1; _i<${#_inarr[*]}; _i++)); do
_outarr[_i]=${_inarr[_i][*]}
done
IFS=$oldIFS
}
######
# main #
######
typeset -a flatarr output
_flatten nums flatarr
integer i j
for ((i=0; i<${#nums[0][*]}; i++)); do
integer cnt=0
for ((j=1; j<=${#flatarr[*]}; j++)); do
[[ ${nums[0][i]} == @(${flatarr[j]%\|}) ]] && (( cnt++ ))
done
(( cnt == 2 )) && output+=( ${nums[0][i]} )
done
print "( ${output[@]} )"</syntaxhighlight>
{{out}}
<pre>( 3 9 6 )</pre>
=={{header|Julia}}==
<
julia> intersect([2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9])
3-element Array{Int64,1}:
Line 487 ⟶ 918:
9
6
</syntaxhighlight>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def intersection
{def intersection.r
{lambda {:a :b :c :d}
{if {A.empty? :a}
then :d
else {intersection.r {A.rest :a} :b :c
{if {and {> {A.in? {A.first :a} :b} -1}
{> {A.in? {A.first :a} :c} -1}}
then {A.addlast! {A.first :a} :d}
else :d} }}}}
{lambda {:a :b :c}
{A.sort! < {intersection.r :a :b :c {A.new}}} }}
-> intersection
{intersection
{A.new 2 5 1 3 8 9 4 6}
{A.new 3 5 6 2 9 8 4}
{A.new 1 3 7 6 9}
}
-> [3,6,9]
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{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}}==
<
proc commonElements(list: openArray[seq[int]]): seq[int] =
Line 506 ⟶ 974:
result.add val
echo commonElements([@[2,5,1,3,8,9,4,6], @[3,5,6,2,9,8,4], @[1,3,7,6,9]])</
{{out}}
Line 512 ⟶ 980:
=={{header|Perl}}==
<
map { print "$_ " if @nums == ++$c{$_} } @$_ for @nums;</
{{out}}
<pre>3 6 9</pre>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">intersection</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 538 ⟶ 1,006:
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">}})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">intersection</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}})</span>
<!--</
Note that a (slightly more flexible) intersection() function is also defined in sets.e, so you could just include that instead, and use it the same way.
{{out}}
Line 549 ⟶ 1,017:
===Without Duplicates===
<
def common_list_elements(*lists):
Line 564 ⟶ 1,032:
result = common_list_elements(*case)
print(f"Intersection of {case} is {result}")
</syntaxhighlight>
{{out}}
Line 573 ⟶ 1,041:
===With Duplicates===
<
from collections import Counter
Line 595 ⟶ 1,063:
result = common_list_elements(*case)
print(f"Intersection of {case} is {result}")
</syntaxhighlight>
{{out}}
Line 605 ⟶ 1,073:
=={{header|Quackery}}==
<
[ sort [] temp put
[ over [] !=
Line 619 ⟶ 1,087:
2drop temp take ] ] is common ( [ [ --> [ )
' [ [ 2 5 1 3 8 9 4 6 ] [ 3 5 6 2 9 8 4 ] [ 1 3 7 6 9 ] ] common echo</
{{out}}
Line 626 ⟶ 1,094:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre>6 9 3</pre>
Line 632 ⟶ 1,100:
=={{header|REXX}}==
This REXX version properly handles the case of duplicate entries in a list (which shouldn't happen in a true list).
<
parse arg a /*obtain optional arguments from the CL*/
if a='' | a="," then a= '[2,5,1,3,8,9,4,6] [3,5,6,2,9,8,4] [1,3,7,6,9]' /*defaults.*/
Line 649 ⟶ 1,117:
end /*k*/
/*stick a fork in it, we're all done. */
say 'the list of common elements in all sets: ' "["translate(space($), ',', " ")']'</
{{out|output|text= when using the default inputs:}}
<pre>
Line 656 ⟶ 1,124:
=={{header|Ring}}==
<
nums = [[2,5,1,3,8,9,4,6],[3,5,6,2,9,8,4],[1,3,7,6,9]]
sumNums = []
Line 709 ⟶ 1,177:
txt = txt + "]"
see txt
</syntaxhighlight>
{{out}}
<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>
=={{header|Ruby}}==
<
p nums.inject(&:intersection) # or nums.inject(:&)
</syntaxhighlight>
{{out}}
<pre>[3, 9, 6]
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn index_of(l []int, n int) int {
for i in 0..l.len {
if l[i] == n {
return i
}
}
return -1
}
fn common2(l1 []int, l2 []int) []int {
// minimize number of lookups
c1, c2 := l1.len, l2.len
mut shortest, mut longest := l1.clone(), l2.clone()
if c1 > c2 {
shortest, longest = l2.clone(), l1.clone()
}
mut longest2 := longest.clone()
mut res := []int{}
for e in shortest {
ix := index_of(longest2, e)
if ix >= 0 {
res << e
longest2 << longest2[ix+1..]
}
}
return res
}
fn common_n(ll [][]int) []int {
n := ll.len
if n == 0 {
return []int{}
}
if n == 1 {
return ll[0]
}
mut res := common2(ll[0], ll[1])
if n == 2 {
return res
}
for l in ll[2..] {
res = common2(res, l)
}
return res
}
fn main() {
lls := [
[[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]],
[[2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]],
]
for ll in lls {
println("Intersection of $ll is:")
println(common_n(ll))
println('')
}
}</syntaxhighlight>
{{out}}
<pre>Intersection of [[2, 5, 1, 3, 8, 9, 4, 6] [3, 5, 6, 2, 9, 8, 4] [1, 3, 7, 6, 9]] is:
[3, 6, 9]
Intersection of [[2 2 1 3 8 9 4 6] [3 5 6 2 2 2 4] [2, 3, 7, 6, 2]] is:
[3, 6, 2, 2]</pre>
'''Alternative:'''
<syntaxhighlight lang="vlang">fn main()
{
lls := [
[[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]],
[[2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]],
]
for ll in lls {
println("Intersection of $ll is:")
println(common_list_elements(ll))
println('')
}
}
fn common_list_elements(md_arr [][]int) []int {
mut counter := map[int]int{}
mut output := []int{}
for sd_arr in md_arr {
for value in sd_arr {
if counter[value] == counter[value] {counter[value] = counter[value] + 1} else { 1 } {
if counter[value] >= md_arr.len && output.any(it == value) == false {
output << value
}
}
}
}
return output
}</syntaxhighlight>
{{out}}
<pre>Intersection of [[2, 5, 1, 3, 8, 9, 4, 6] [3, 5, 6, 2, 9, 8, 4] [1, 3, 7, 6, 9]] is:
[3, 6, 9]
Intersection of [[2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]] is:
[2, 3, 6]</pre>
=={{header|Wren}}==
{{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.
<
var common2 = Fn.new { |l1, l2|
Line 763 ⟶ 1,363:
System.print(commonN.call(ll))
System.print()
}</
{{out}}
Line 772 ⟶ 1,372:
Intersection of [[2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]] is:
[2, 3, 6, 2]
</pre>
<br>
Since the above was written, we can also now offer a library based solution.
<syntaxhighlight lang="wren">import "./seq" for Lst
var lls = [
[[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]],
[[2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]]
]
for (ll in lls) {
System.print(Lst.intersect(ll[0], Lst.intersect(ll[1], ll[2])))
}</syntaxhighlight>
{{out}}
<pre>
[3, 9, 6]
[2, 2, 3, 6]
</pre>
=={{header|XPL0}}==
A 32-bit integer is used to specify a set of values 0..31.
The [-1] terminator helps determine the number of lists.
<syntaxhighlight lang="xpl0">int IntSize, Nums, Sets, Ans, N, ListSize, Set, I;
[IntSize:= @Nums - @IntSize; \number of bytes in an integer
Nums:= [[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9], [-1]];
Sets:= 0; \find the number of lists = number of Sets
while Nums(Sets, 0) > 0 do Sets:= Sets+1;
Ans:= -1;
for N:= 0 to Sets-1 do
[ListSize:= (Nums(N+1) - Nums(N)) / IntSize;
Set:= 0;
for I:= 0 to ListSize-1 do \Set = union (or) of list elements
Set:= Set or 1<<Nums(N, I);
Ans:= Ans & Set; \Answer is intersection (&) of Sets
];
I:= 0;
while Ans do \show common list elements
[if Ans & 1 then
[IntOut(0, I); ChOut(0, ^ )];
Ans:= Ans>>1;
I:= I+1;
];
]</syntaxhighlight>
{{out}}
<pre>
3 6 9
</pre>
|