Common list elements: Difference between revisions
Content added Content deleted
(J) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13: | Line 13: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F cle(nums) |
||
V r = Set(nums[0]) |
V r = Set(nums[0]) |
||
L(num) nums[1..] |
L(num) nums[1..] |
||
Line 19: | Line 19: | ||
R r |
R r |
||
print(cle([[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]]))</ |
print(cle([[2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 28: | Line 28: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit |
||
DEFINE PTR="CARD" |
DEFINE PTR="CARD" |
||
Line 135: | Line 135: | ||
arrays(2)=a7 |
arrays(2)=a7 |
||
Test(arrays,lengths,3) |
Test(arrays,lengths,3) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Common_list_elements.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Common_list_elements.png Screenshot from Atari 8-bit computer] |
||
Line 162: | Line 162: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
with Ada.Containers.Vectors; |
with Ada.Containers.Vectors; |
||
Line 202: | Line 202: | ||
R := Common_Elements (R, C); |
R := Common_Elements (R, C); |
||
Put (R); |
Put (R); |
||
end Common;</ |
end Common;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ 3 9 6 ]</pre> |
<pre>[ 3 9 6 ]</pre> |
||
Line 209: | Line 209: | ||
APL has the built-in intersection function <code>∩</code> |
APL has the built-in intersection function <code>∩</code> |
||
< |
<syntaxhighlight lang="apl"> ∩/ (2 5 1 3 8 9 4 6) (3 5 6 2 9 8 4) (1 3 7 9 6) |
||
3 9 6 </ |
3 9 6 </syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===AppleScriptObjC=== |
===AppleScriptObjC=== |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
||
use framework "Foundation" |
use framework "Foundation" |
||
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript> |
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript> |
||
Line 229: | Line 229: | ||
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}}) |
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) |
tell sorter to sort(commonElements, 1, -1) |
||
return commonElements</ |
return commonElements</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<lang |
<syntaxhighlight lang="applescript">{3, 6, 9}</syntaxhighlight> |
||
===Core language only=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later. |
||
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript> |
use sorter : script "Insertion Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript> |
||
Line 262: | Line 262: | ||
set commonElements to commonListElements({{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}}) |
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) |
tell sorter to sort(commonElements, 1, -1) |
||
return commonElements</ |
return commonElements</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">commonElements: function [subsets][ |
||
if zero? size subsets -> return [] |
if zero? size subsets -> return [] |
||
if 1 = size subsets -> return first subsets |
if 1 = size subsets -> return first subsets |
||
Line 282: | Line 282: | ||
[3 5 6 2 9 8 4] |
[3 5 6 2 9 8 4] |
||
[1 3 7 6 9] |
[1 3 7 6 9] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 289: | Line 289: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Common_list_elements(nums){ |
||
counter := [], output := [] |
counter := [], output := [] |
||
for i, num in nums |
for i, num in nums |
||
Line 297: | Line 297: | ||
return output |
return output |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">nums := [[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]] |
||
output := Common_list_elements(nums) |
output := Common_list_elements(nums) |
||
return</ |
return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[3, 6, 9]</pre> |
<pre>[3, 6, 9]</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f COMMON_LIST_ELEMENTS.AWK |
# syntax: GAWK -f COMMON_LIST_ELEMENTS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 327: | Line 327: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 333: | Line 333: | ||
</pre> |
</pre> |
||
=={{header|CLU}}== |
=={{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) |
where T has equal: proctype (T,T) returns (bool) |
||
for i: T in array[T]$elements(a) do |
for i: T in array[T]$elements(a) do |
||
Line 374: | Line 374: | ||
stream$puts(po, int$unparse(i) || " ") |
stream$puts(po, int$unparse(i) || " ") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 6 9</pre> |
<pre>3 6 9</pre> |
||
Line 386: | Line 386: | ||
{{Works with|Office 365 Betas 2021}} |
{{Works with|Office 365 Betas 2021}} |
||
< |
<syntaxhighlight lang="lisp">INTERSECT |
||
=LAMBDA(xs, |
=LAMBDA(xs, |
||
LAMBDA(ys, |
LAMBDA(ys, |
||
Line 410: | Line 410: | ||
xs |
xs |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
and also assuming the following generic bindings in Name Manager: |
and also assuming the following generic bindings in Name Manager: |
||
< |
<syntaxhighlight lang="lisp">ELEM |
||
=LAMBDA(x, |
=LAMBDA(x, |
||
LAMBDA(xs, |
LAMBDA(xs, |
||
Line 453: | Line 453: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 544: | Line 544: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Of course it is possible to use sets but I thought the idea was not to? |
Of course it is possible to use sets but I thought the idea was not to? |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Common list elements. Nigel Galloway: February 25th., 2021 |
// 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]|] |
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)));; |
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> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 557: | Line 557: | ||
Note: in older versions of Factor, <code>intersect-all</code> was called <code>intersection</code>. |
Note: in older versions of Factor, <code>intersect-all</code> was called <code>intersection</code>. |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: prettyprint sets ; |
||
{ { 2 5 1 3 8 9 4 6 } { 3 5 6 2 9 8 4 } { 1 3 7 6 9 } } intersect-all .</ |
{ { 2 5 1 3 8 9 4 6 } { 3 5 6 2 9 8 4 } { 1 3 7 6 9 } } intersect-all .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 566: | Line 566: | ||
=={{header|FreeBASIC}}== |
=={{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) |
redim as integer outp(0) |
||
dim as integer i, j |
dim as integer i, j |
||
Line 591: | Line 591: | ||
for i = 1 to ubound(outp) |
for i = 1 to ubound(outp) |
||
print outp(i);" "; |
print outp(i);" "; |
||
next i</ |
next i</syntaxhighlight> |
||
{{out}}<pre>3 9 6</pre> |
{{out}}<pre>3 9 6</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 657: | Line 657: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 669: | Line 669: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Set as Set |
||
task :: Ord a => [[a]] -> [a] |
task :: Ord a => [[a]] -> [a] |
||
Line 675: | Line 675: | ||
task xs = Set.toAscList . foldl1 Set.intersection . map Set.fromList $ xs |
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]]</ |
main = print $ task [[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[3,6,9]</pre> |
<pre>[3,6,9]</pre> |
||
Line 681: | Line 681: | ||
=={{header|J}}== |
=={{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</ |
3 9 6</syntaxhighlight> |
||
Or, |
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</ |
3 9 6</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 696: | Line 696: | ||
on the arrays whose intersection is sought. The helper function, `ios`, |
on the arrays whose intersection is sought. The helper function, `ios`, |
||
might be independently useful and so is defined as a top-level filter. |
might be independently useful and so is defined as a top-level filter. |
||
< |
<syntaxhighlight lang="jq"># If a and b are sorted lists, and if all the elements respectively of a and b are distinct, |
||
# then [a,b] | ios will emit the stream of elements in the set-intersection of a and b. |
# then [a,b] | ios will emit the stream of elements in the set-intersection of a and b. |
||
def ios: |
def ios: |
||
Line 716: | Line 716: | ||
elif any(.[]; length == 0) then [] |
elif any(.[]; length == 0) then [] |
||
else sort_by(length) | go |
else sort_by(length) | go |
||
end;</ |
end;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq"># The task: |
||
[[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</ |
[[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 725: | Line 725: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 768: | Line 768: | ||
done |
done |
||
print "( ${output[@]} )"</ |
print "( ${output[@]} )"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>( 3 9 6 )</pre> |
<pre>( 3 9 6 )</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"> |
||
julia> intersect([2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]) |
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}: |
3-element Array{Int64,1}: |
||
Line 779: | Line 779: | ||
9 |
9 |
||
6 |
6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
<syntaxhighlight lang="scheme"> |
|||
<lang Scheme> |
|||
{def intersection |
{def intersection |
||
Line 805: | Line 805: | ||
} |
} |
||
-> [3,6,9] |
-> [3,6,9] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Intersection[{2, 5, 1, 3, 8, 9, 4, 6}, {3, 5, 6, 2, 9, 8, 4}, {1, 3, 7, 6, 9}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{3, 6, 9}</pre> |
<pre>{3, 6, 9}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, sequtils |
||
proc commonElements(list: openArray[seq[int]]): seq[int] = |
proc commonElements(list: openArray[seq[int]]): seq[int] = |
||
Line 824: | Line 824: | ||
result.add val |
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]])</ |
echo commonElements([@[2,5,1,3,8,9,4,6], @[3,5,6,2,9,8,4], @[1,3,7,6,9]])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 830: | Line 830: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">@nums = ([2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]); |
||
map { print "$_ " if @nums == ++$c{$_} } @$_ for @nums;</ |
map { print "$_ " if @nums == ++$c{$_} } @$_ for @nums;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 6 9</pre> |
<pre>3 6 9</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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: #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> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
Line 856: | Line 856: | ||
<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;">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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
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. |
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}} |
{{out}} |
||
Line 867: | Line 867: | ||
===Without Duplicates=== |
===Without Duplicates=== |
||
< |
<syntaxhighlight lang="python">"""Find distinct common list elements using set.intersection.""" |
||
def common_list_elements(*lists): |
def common_list_elements(*lists): |
||
Line 882: | Line 882: | ||
result = common_list_elements(*case) |
result = common_list_elements(*case) |
||
print(f"Intersection of {case} is {result}") |
print(f"Intersection of {case} is {result}") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 891: | Line 891: | ||
===With Duplicates=== |
===With Duplicates=== |
||
< |
<syntaxhighlight lang="python">"""Find common list elements using collections.Counter (multiset).""" |
||
from collections import Counter |
from collections import Counter |
||
Line 913: | Line 913: | ||
result = common_list_elements(*case) |
result = common_list_elements(*case) |
||
print(f"Intersection of {case} is {result}") |
print(f"Intersection of {case} is {result}") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 923: | Line 923: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ behead sort swap witheach |
||
[ sort [] temp put |
[ sort [] temp put |
||
[ over [] != |
[ over [] != |
||
Line 937: | Line 937: | ||
2drop temp take ] ] is common ( [ [ --> [ ) |
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</ |
' [ [ 2 5 1 3 8 9 4 6 ] [ 3 5 6 2 9 8 4 ] [ 1 3 7 6 9 ] ] common echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 944: | Line 944: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>put [∩] [2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9,3];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>6 9 3</pre> |
<pre>6 9 3</pre> |
||
Line 950: | Line 950: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX version properly handles the case of duplicate entries in a list (which shouldn't happen in a true list). |
This REXX version properly handles the case of duplicate entries in a list (which shouldn't happen in a true list). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds and displays the common list elements from a collection of sets. */ |
||
parse arg a /*obtain optional arguments from the CL*/ |
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.*/ |
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 967: | Line 967: | ||
end /*k*/ |
end /*k*/ |
||
/*stick a fork in it, we're all done. */ |
/*stick a fork in it, we're all done. */ |
||
say 'the list of common elements in all sets: ' "["translate(space($), ',', " ")']'</ |
say 'the list of common elements in all sets: ' "["translate(space($), ',', " ")']'</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 974: | Line 974: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
nums = [[2,5,1,3,8,9,4,6],[3,5,6,2,9,8,4],[1,3,7,6,9]] |
nums = [[2,5,1,3,8,9,4,6],[3,5,6,2,9,8,4],[1,3,7,6,9]] |
||
sumNums = [] |
sumNums = [] |
||
Line 1,027: | Line 1,027: | ||
txt = txt + "]" |
txt = txt + "]" |
||
see txt |
see txt |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,034: | Line 1,034: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">nums = [2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9] |
||
p nums.inject(&:intersection) # or nums.inject(:&) |
p nums.inject(&:intersection) # or nums.inject(:&) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[3, 9, 6] |
<pre>[3, 9, 6] |
||
Line 1,043: | Line 1,043: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">fn index_of(l []int, n int) int { |
||
for i in 0..l.len { |
for i in 0..l.len { |
||
if l[i] == n { |
if l[i] == n { |
||
Line 1,099: | Line 1,099: | ||
println('') |
println('') |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{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: |
<pre>Intersection of [[2, 5, 1, 3, 8, 9, 4, 6] [3, 5, 6, 2, 9, 8, 4] [1, 3, 7, 6, 9]] is: |
||
Line 1,108: | Line 1,108: | ||
'''Alternative:''' |
'''Alternative:''' |
||
< |
<syntaxhighlight lang="vlang">fn main() |
||
{ |
{ |
||
lls := [ |
lls := [ |
||
Line 1,136: | Line 1,136: | ||
} |
} |
||
return output |
return output |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{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: |
<pre>Intersection of [[2, 5, 1, 3, 8, 9, 4, 6] [3, 5, 6, 2, 9, 8, 4] [1, 3, 7, 6, 9]] is: |
||
Line 1,147: | Line 1,147: | ||
{{libheader|Wren-seq}} |
{{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. |
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="ecmascript">import "/seq" for Lst |
||
var common2 = Fn.new { |l1, l2| |
var common2 = Fn.new { |l1, l2| |
||
Line 1,185: | Line 1,185: | ||
System.print(commonN.call(ll)) |
System.print(commonN.call(ll)) |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,197: | Line 1,197: | ||
<br> |
<br> |
||
Since the above was written, we can also now offer a library based solution. |
Since the above was written, we can also now offer a library based solution. |
||
< |
<syntaxhighlight lang="ecmascript">import "./seq" for Lst |
||
var lls = [ |
var lls = [ |
||
Line 1,206: | Line 1,206: | ||
for (ll in lls) { |
for (ll in lls) { |
||
System.print(Lst.intersect(ll[0], Lst.intersect(ll[1], ll[2]))) |
System.print(Lst.intersect(ll[0], Lst.intersect(ll[1], ll[2]))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,217: | Line 1,217: | ||
A 32-bit integer is used to specify a set of values 0..31. |
A 32-bit integer is used to specify a set of values 0..31. |
||
The [-1] terminator helps determine the number of lists. |
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 |
[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]]; |
Nums:= [[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9], [-1]]; |
||
Line 1,237: | Line 1,237: | ||
I:= I+1; |
I:= I+1; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |