Common sorted list: Difference between revisions
→{{header|Free Pascal}}
(15 intermediate revisions by 13 users not shown) | |||
Line 13:
=={{header|11l}}==
<
Set[Int] r
L(num) nums
Line 19:
R sorted(Array(r))
print(csl([[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]]))</
{{out}}
Line 28:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
DEFINE PTR="CARD"
Line 123:
lengths(0)=0
Test(arrays,lengths,3)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Common_sorted_list.png Screenshot from Atari 8-bit computer]
Line 150:
=={{header|Ada}}==
<
with Ada.Containers.Vectors;
Line 194:
Unique (R);
Put (R);
end Sorted;</
{{out}}
<pre>[ 1 3 4 5 7 8 9 ]</pre>
Line 200:
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="text">csl ← (⊂∘⍋⌷⊢)∪∘∊</
{{out}}
<pre> csl (5 1 3 8 9 4 8 7)(3 5 9 8 4)(1 3 7 9)
Line 207:
=={{header|AppleScript}}==
<
use framework "Foundation"
Line 214:
set nums to current application's class "NSArray"'s arrayWithArray:({{5, 1, 3, 8, 9, 4, 8, 7}, {3, 5, 9, 8, 4}, {1, 3, 7, 9}})
set output to (nums's valueForKeyPath:("@distinctUnionOfArrays.self"))'s sortedArrayUsingSelector:("compare:")
return output as list</
{{output}}
<
Line 224:
while ''concat/flatten'' and ''nub/distinct'' are more atomic, and more frequently reached for):
<
use framework "Foundation"
Line 257:
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</
{{Out}}
<pre>{1, 3, 4, 5, 7, 8, 9}</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">print sort unique flatten [[5 1 3 8 9 4 8 7] [3 5 9 8 4] [1 3 7 9]]</syntaxhighlight>
{{out}}
<pre>1 3 4 5 7 8 9</pre>
=={{header|AutoHotkey}}==
<
elements := [], output := []
for i, num in nums
Line 270 ⟶ 278:
output.push(val)
return output
}</
Examples:<
output := Common_sorted_list(nums)
return</
{{out}}
<pre>[1, 3, 4, 5, 6, 7, 9]</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COMMON_SORTED_LIST.AWK
BEGIN {
Line 298 ⟶ 306:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 308 ⟶ 316:
Joins all the arrays, deduplicate, sort.
<
⟨ ⟨ 5 1 3 8 9 4 8 7 ⟩ ⟨ 3 5 9 8 4 ⟩ ⟨ 1 3 7 9 ⟩ ⟩
CSL ← ∧·⍷∾
∧(⍷∾)
CSL arr
⟨ 1 3 4 5 7 8 9 ⟩</
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 386 ⟶ 394:
free(sorted);
return 0;
}</
{{out}}
Line 394 ⟶ 402:
=={{header|C++}}==
<
#include <vector>
#include <set>
Line 424 ⟶ 432:
return 0;
}</
{{out}}
<pre>1 3 4 5 7 8 9</pre>
=={{header|Clojure}}==
===Efficient transducer & sorted-set version===
<syntaxhighlight lang="lisp">(defn common-sorted [& colls]
(into (sorted-set) cat colls))
(common-sorted [5 1 3 8 9 4 8 7] [3 5 9 8 4] [1 3 7 9])
;; => #{1 3 4 5 7 8 9}
</syntaxhighlight>
===Point-free, list-based version===
<syntaxhighlight lang="lisp">(def common-sorted (comp sort distinct concat))
(common-sorted [5 1 3 8 9 4 8 7] [3 5 9 8 4] [1 3 7 9])
;; => (1 3 4 5 7 8 9)
</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight>
nums[][] = [ [ 2 5 1 3 8 9 4 6 ] [ 3 5 6 2 9 8 4 ] [ 1 3 7 6 9 ] ]
#
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
#
for l to len nums[][]
for e in nums[l][]
h[] &= e
.
.
sort h[]
for e in h[]
if e <> last or len r[] = 0
r[] &= e
last = e
.
.
print r[]
</syntaxhighlight>
=={{header|Excel}}==
Line 438 ⟶ 492:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(grid,
SORT(
Line 445 ⟶ 499:
)
)
)</
and also assuming the following generic bindings in the Name Manager for the WorkBook:
<
=LAMBDA(cols,
LET(
Line 479 ⟶ 533:
FILTER(xs, p(xs))
)
)</
The formula in cell B2 defines a dynamic array of values, additionally populating further cells in column B.
Line 560 ⟶ 614:
=={{header|F_Sharp|F#}}==
<
// Common sorted list. Nigel Galloway: February 25th., 2021
let nums=[|[5;1;3;8;9;4;8;7];[3;5;9;8;4];[1;3;7;9]|]
printfn "%A" (nums|>Array.reduce(fun n g->n@g)|>List.distinct|>List.sort)
</syntaxhighlight>
{{out}}
<pre>
Line 573 ⟶ 627:
Note: in older versions of Factor, <code>union-all</code> is called <code>combine</code>.
{{works with|Factor|0.99 2021-02-05}}
<
{ { 5 1 3 8 9 4 8 7 } { 3 5 9 8 4 } { 1 3 7 9 } }
dup union-all natural-sort
"Sorted union of %u is:\n%u\n" printf</
{{out}}
<pre>
Line 583 ⟶ 637:
{ 1 3 4 5 7 8 9 }
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Dim As Integer nums(2, 7) = {{5,1,3,8,9,4,8,7},{3,5,9,8,4},{1,3,7,9}}
Dim Shared As Integer sumNums()
Sub Sum(arr() As Integer, vals As Integer)
Redim Preserve arr(Ubound(arr) + 1)
arr(Ubound(arr)) = vals
End Sub
Sub Del(arr() As Integer, index As Integer)
For i As Integer = index To Ubound(arr) - 1
arr(i) = arr(i + 1)
Next i
Redim Preserve arr(Ubound(arr) - 1)
End Sub
Sub showArray(arr() As Integer)
Dim As String txt = ""
Print "[";
For n As Integer = 1 To Ubound(arr)
txt &= Str(arr(n)) & ","
Next n
txt = Left(txt, Len(txt) - 1)
txt &= "]"
Print txt
End Sub
Sub Sort(arr() As Integer)
Dim As Integer i, j
For i = 0 To Ubound(arr) - 1
For j = i + 1 To Ubound(arr)
If arr(i) > arr(j) Then Swap arr(i), arr(j)
Next j
Next i
End Sub
For n As Integer = 0 To Ubound(nums, 1)
For m As Integer = 0 To Ubound(nums, 2)
Sum(sumNums(), nums(n, m))
Next m
Next n
Sort(sumNums())
For n As Integer = Ubound(sumNums) To 1 Step -1
If sumNums(n) = sumNums(n - 1) Then
Del(sumNums(), n)
End If
Next n
Sort(sumNums())
Print "common sorted list elements are: ";
showArray(sumNums())
Sleep</syntaxhighlight>
{{out}}
<pre>common sorted list elements are: [1,3,4,5,7,8,9]</pre>
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 614 ⟶ 726:
fmt.Println("Distinct sorted union of", ll, "is:")
fmt.Println(distinctSortedUnion(ll))
}</
{{out}}
Line 623 ⟶ 735:
=={{header|Haskell}}==
<
-------------------- COMMON SORTED LIST ------------------
Line 638 ⟶ 750:
[3, 5, 9, 8, 4],
[1, 3, 7, 9]
]</
{{Out}}
<pre>[1,3,4,5,7,8,9]</pre>
=={{header|J}}==
<
{{out}}
<pre> nums =: 5 1 3 8 9 4 8 7;3 5 9 8 4;1 3 7 9
Line 650 ⟶ 762:
=={{header|JavaScript}}==
<
"use strict";
Line 672 ⟶ 784:
// concat :: [[a]] -> [a]
const concat = xs =>
xs.flat(1);
// nub :: Eq a => [a] -> [a]
const nub = xs => [...new Set(xs)];
Line 685 ⟶ 798:
return main();
})();</
{{Out}}
<pre>[1, 3, 4, 5, 7, 8, 9]</pre>
Line 694 ⟶ 807:
Assuming the input is a single array of arrays, we can simply chain the `add` and `unique` filters:
<
{{out}}
<pre>
Line 702 ⟶ 815:
Alternatively, here's a stream-oriented version of `add` that could be used:
<
For example:
<
def add(s): reduce s as $x (null; . + $x);
add(inputs) | unique' <<< '[5,1,3,8,9,4,8,7] [3,5,9,8,4] [1,3,7,9]'</
=={{header|Julia}}==
<
julia> sort(union([5,1,3,8,9,4,8,7], [3,5,9,8,4], [1,3,7,9]))
7-element Array{Int64,1}:
Line 730 ⟶ 843:
"not"
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{1, 3, 4, 5, 7, 8, 9}</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
common_sorted(lst):=unique(flatten(lst))$
nums:[[5,1,3,8,9,4,8,7],[3,5,9,8,4],[1,3,7,9]]$
common_sorted(nums);
</syntaxhighlight>
{{out}}
<pre>
[1,3,4,5,7,8,9]
</pre>
=={{header|Nim}}==
We could use a <code>HashSet</code> or an <code>IntSet</code> to deduplicate. We have rather chosen to use the procedure <code>deduplicate</code> from module <code>sequtils</code> applied to the sorted list.
<
proc commonSortedList(list: openArray[seq[int]]): seq[int] =
sorted(concat(list)).deduplicate(true)
echo commonSortedList([@[5,1,3,8,9,4,8,7], @[3,5,9,8,4], @[1,3,7,9]])</
{{out}}
<pre>@[1, 3, 4, 5, 7, 8, 9]</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program CommonSortedList;
{$mode ObjFPC}{$H+}
Uses sysutils,fgl;
Type
tarr = array Of array Of integer;
Const List1: tarr = ((5,1,3,8,9,4,8,7), (3,5,9,8,4), (1,3,7,9));
Var list : specialize TFPGList<integer>;
Procedure addtolist(arr : tarr);
Var i : integer;
arr2 : array Of integer;
Begin
For arr2 In arr Do
For i In arr2 Do
If (list.indexof(i) = -1) {make sure number isn't in list already}
Then list.add(i);
End;
Function CompareInt(Const Item1,Item2: Integer): Integer;
Begin
result := item1 - item2;
End;
Var i : integer;
Begin
list := specialize TFPGList<integer>.create;
addtolist(list1);
List.Sort(@CompareInt); {quick sort the list}
For i In list Do
write(i:4);
list.destroy;
End.
</syntaxhighlight>
{{out}}
<pre>
1 3 4 5 7 8 9
</pre>
=={{header|Perl}}==
<
print join ' ', sort keys %c;
@c{@$_}++ for [qw<not all is integer ? is not ! 4.2>];
print join ' ', sort keys %c;</
{{out}}
<pre>1 3 4 5 7 8 9
Line 761 ⟶ 930:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</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;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</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;">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: #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;">9</span><span style="color: #0000FF;">},</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"not everything is an integer"</span><span style="color: #0000FF;">)},{}))</span>
<!--</
Note the join(x,{}): the 2nd param is needed to prevent it putting 32 (ie the acsii code for a space) in the output.
{{out}}
Line 772 ⟶ 941:
=={{header|Python}}==
<
from itertools import chain
Line 812 ⟶ 981:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>[1, 3, 4, 5, 7, 8, 9]</pre>
Line 820 ⟶ 989:
<code>uniquewith</code> is defined at [[Remove duplicate elements#Quackery]].
<
[ 3 5 9 8 4 ]
[ 1 3 7 9 ] ]
[] swap witheach [ witheach join ]
uniquewith > echo</
{{out}}
Line 833 ⟶ 1,002:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
put sort keys [∪] [5,1,3,8,9,4,8,7], [3,5,9,8,4], [1,3,7,9], [<not everything is an integer so why not avoid special cases # ~ 23 4.2>];</
{{out}}
<pre>1 3 4 5 7 8 9
Line 840 ⟶ 1,009:
=={{header|REXX}}==
<
parse arg a /*obtain optional arguments from the CL*/
if a='' | a="," then a= '[5,1,3,8,9,4,8,7] [3,5,9,8,4] [1,3,7,9]' /*default sets.*/
Line 861 ⟶ 1,030:
eSort: procedure expose @.; arg h 1 z; do while h>1; h= h%2; do i=1 for z-h; j= i; k= h+i
do while @.k<@.j; t=@.j; @.j=@.k; @.k=t; if h>=j then leave; j=j-h; k=k-h; end;end
end; return /*this sort was used 'cause of brevity.*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 868 ⟶ 1,037:
=={{header|Ring}}==
<
nums = [[5,1,3,8,9,4,8,7],[3,5,9,8,4],[1,3,7,9]]
sumNums = []
Line 899 ⟶ 1,068:
txt = txt + "]"
see txt
</syntaxhighlight>
{{out}}
<pre>
common sorted list elements are: [1,3,4,5,7,8,9]
</pre>
=={{header|RPL}}==
{{works with|HP|48G}}
≪ ∑LIST SORT → nums
≪ <span style="color:red">{ }</span>
<span style="color:red">1</span> nums SIZE '''FOR''' j
nums j GET
'''IF''' DUP2 POS '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">COMMON</span>' STO
<span style="color:red">{ { 5 1 3 8 9 4 8 7 } { 3 5 9 8 4 } { 1 3 7 9 } }</span> <span style="color:blue">COMMON</span>
{{out}}
<pre>
1: { 1 3 4 5 7 8 9 }
</pre>
=={{header|Ruby}}==
<
p nums.inject(:+).sort.uniq
</syntaxhighlight>
{{out}}
<pre>[1, 3, 4, 5, 7, 8, 9]
</pre>
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
F ← ⊏⍏.◴/◇⊂
</syntaxhighlight>
{{out}}
<pre>
F {[5 1 3 8 9 4 8 7] [3 5 9 8 4] [1 3 7 9]}
[1 3 4 5 7 8 9]
</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
fn main() {
nums := [[5,1,3,8,9,4,8,7], [3,5,9,8,4], [1,3,7,9]]
println("Distinct sorted union of $nums is:")
println(common_sorted_list(nums))
}
fn common_sorted_list(nums [][]int) []int {
mut elements, mut output := map[int]bool{}, []int{}
for num in nums {
for value in num {
elements[value] = true
}
}
for key, _ in elements {
output << key
}
output.sort()
return output
}
</syntaxhighlight>
{{out}}
<pre>
Distinct sorted union of [[5 1 3 8 9 4 8 7] [3 5 9 8 4] [1 3 7 9]] is:
[1 3 4 5 7 8 9]
</pre>
=={{header|Wren}}==
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<
import "./sort" for Sort
var distinctSortedUnion = Fn.new { |ll|
Line 927 ⟶ 1,153:
var ll = [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]]
System.print("Distinct sorted union of %(ll) is:")
System.print(distinctSortedUnion.call(ll))</
{{out}}
|