tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
6
Task/Remove-duplicate-elements/0DESCRIPTION
Normal file
6
Task/Remove-duplicate-elements/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Given an Array, derive a sequence of elements in which all duplicates are removed.
|
||||
|
||||
There are basically three approaches seen here:
|
||||
* Put the elements into a hash table which does not allow duplicates. The complexity is O(''n'') on average, and O(''n''<sup>2</sup>) worst case. This approach requires a hash function for your type (which is compatible with equality), either built-in to your language, or provided by the user.
|
||||
* Sort the elements and remove consecutive duplicate elements. The complexity of the best sorting algorithms is O(''n'' log ''n''). This approach requires that your type be "comparable", i.e., have an ordering. Putting the elements into a self-balancing binary search tree is a special case of sorting.
|
||||
* Go through the list, and for each element, check the rest of the list to see if it appears again, and discard it if it does. The complexity is O(''n''<sup>2</sup>). The up-shot is that this always works on any type (provided that you can test for equality).
|
||||
|
|
@ -0,0 +1 @@
|
|||
(remove-duplicates xs)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ awk 'BEGIN{split("a b c d c b a",a);for(i in a)b[a[i]]=1;r="";for(i in b)r=r" "i;print r}'
|
||||
a b c d
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
with Ada.Containers.Ordered_Sets;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Unique_Set is
|
||||
package Int_Sets is new Ada.Containers.Ordered_Sets(Integer);
|
||||
use Int_Sets;
|
||||
Nums : array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
|
||||
Unique : Set;
|
||||
Set_Cur : Cursor;
|
||||
Success : Boolean;
|
||||
begin
|
||||
for I in Nums'range loop
|
||||
Unique.Insert(Nums(I), Set_Cur, Success);
|
||||
end loop;
|
||||
Set_Cur := Unique.First;
|
||||
loop
|
||||
Put_Line(Item => Integer'Image(Element(Set_Cur)));
|
||||
exit when Set_Cur = Unique.Last;
|
||||
Set_Cur := Next(Set_Cur);
|
||||
end loop;
|
||||
end Unique_Set;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
∪ 1 2 3 1 2 3 4 1
|
||||
1 2 3 4
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
w←1 2 3 1 2 3 4 1
|
||||
((⍳⍨w)=⍳⍴w)/w
|
||||
1 2 3 4
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
|
||||
|
||||
on unique(x)
|
||||
set R to {}
|
||||
repeat with i in x
|
||||
if i is not in R then set end of R to i's contents
|
||||
end repeat
|
||||
return R
|
||||
end unique
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a = 1,2,1,4,5,2,15,1,3,4
|
||||
Sort, a, a, NUD`,
|
||||
MsgBox % a ; 1,2,3,4,5,15
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
DIM list$(15)
|
||||
list$() = "Now", "is", "the", "time", "for", "all", "good", "men", \
|
||||
\ "to", "come", "to", "the", "aid", "of", "the", "party."
|
||||
num% = FNremoveduplicates(list$())
|
||||
FOR i% = 0 TO num%-1
|
||||
PRINT list$(i%) " " ;
|
||||
NEXT
|
||||
PRINT
|
||||
END
|
||||
|
||||
DEF FNremoveduplicates(l$())
|
||||
LOCAL i%, j%, n%, i$
|
||||
n% = 1
|
||||
FOR i% = 1 TO DIM(l$(), 1)
|
||||
i$ = l$(i%)
|
||||
FOR j% = 0 TO i%-1
|
||||
IF i$ = l$(j%) EXIT FOR
|
||||
NEXT
|
||||
IF j%>=i% l$(n%) = i$ : n% += 1
|
||||
NEXT
|
||||
= n%
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
2 3 5 7 11 13 17 19 cats 222 (-100.2) "+11" (1.1) "+7" (7.) 7 5 5 3 2 0 (4.4) 2:?LIST
|
||||
|
||||
(A=
|
||||
( Hashing
|
||||
= h elm list
|
||||
. new$hash:?h
|
||||
& whl
|
||||
' ( !arg:%?elm ?arg
|
||||
& ( (h..find)$str$!elm
|
||||
| (h..insert)$(str$!elm.!elm)
|
||||
)
|
||||
)
|
||||
& :?list
|
||||
& (h..forall)
|
||||
$ (
|
||||
= .!arg:(?.?arg)&!arg !list:?list
|
||||
)
|
||||
& !list
|
||||
)
|
||||
& put$("Solution A:" Hashing$!LIST \n,LIN)
|
||||
);
|
||||
|
||||
(B=
|
||||
( backtracking
|
||||
= answr elm
|
||||
. :?answr
|
||||
& !arg
|
||||
: ?
|
||||
( %?`elm
|
||||
?
|
||||
( !elm ?
|
||||
| &!answr !elm:?answr
|
||||
)
|
||||
& ~
|
||||
)
|
||||
| !answr
|
||||
)
|
||||
& put$("Solution B:" backtracking$!LIST \n,LIN)
|
||||
);
|
||||
|
||||
(C=
|
||||
( summing
|
||||
= sum car LIST
|
||||
. !arg:?LIST
|
||||
& 0:?sum
|
||||
& whl
|
||||
' ( !LIST:%?car ?LIST
|
||||
& (.!car)+!sum:?sum
|
||||
)
|
||||
& whl
|
||||
' ( !sum:#*(.?el)+?sum
|
||||
& !el !LIST:?LIST
|
||||
)
|
||||
& !LIST
|
||||
)
|
||||
& put$("Solution C:" summing$!LIST \n,LIN)
|
||||
);
|
||||
|
||||
( !A
|
||||
& !B
|
||||
& !C
|
||||
&
|
||||
)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
some_array = [1 1 2 1 'redundant' [1 2 3] [1 2 3] 'redundant']
|
||||
|
||||
unique_array = some_array.unique
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
#include <set>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
typedef set<int> TySet;
|
||||
int data[] = {1, 2, 3, 2, 3, 4};
|
||||
|
||||
TySet unique_set(data, data + 6);
|
||||
|
||||
cout << "Set items:" << endl;
|
||||
for (TySet::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
|
||||
cout << *iter << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
#include <ext/hash_set>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
typedef __gnu_cxx::hash_set<int> TyHash;
|
||||
int data[] = {1, 2, 3, 2, 3, 4};
|
||||
|
||||
TyHash unique_set(data, data + 6);
|
||||
|
||||
cout << "Set items:" << endl;
|
||||
for (TyHash::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
|
||||
cout << *iter << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
#include <tr1/unordered_set>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
typedef tr1::unordered_set<int> TyHash;
|
||||
int data[] = {1, 2, 3, 2, 3, 4};
|
||||
|
||||
TyHash unique_set(data, data + 6);
|
||||
|
||||
cout << "Set items:" << endl;
|
||||
for (TyHash::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
|
||||
cout << *iter << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
// helper template
|
||||
template<typename T> T* end(T (&array)[size]) { return array+size; }
|
||||
|
||||
int main()
|
||||
{
|
||||
int data[] = { 1, 2, 3, 2, 3, 4 };
|
||||
std::sort(data, end(data));
|
||||
int* new_end = std::unique(data, end(data));
|
||||
std::copy(data, new_end, std::ostream_iterator<int>(std::cout, " ");
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
int[] nums = { 1, 1, 2, 3, 4, 4 };
|
||||
List<int> unique = new List<int>();
|
||||
foreach (int n in nums)
|
||||
if (!unique.Contains(n))
|
||||
unique.Add(n);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
int[] nums = {1, 1, 2, 3, 4, 4};
|
||||
int[] unique = nums.Distinct().ToArray();
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct list_node {int x; struct list_node *next;};
|
||||
typedef struct list_node node;
|
||||
|
||||
node * uniq(int *a, unsigned alen)
|
||||
{if (alen == 0) return NULL;
|
||||
node *start = malloc(sizeof(node));
|
||||
if (start == NULL) exit(EXIT_FAILURE);
|
||||
start->x = a[0];
|
||||
start->next = NULL;
|
||||
|
||||
for (int i = 1 ; i < alen ; ++i)
|
||||
{node *n = start;
|
||||
for (;; n = n->next)
|
||||
{if (a[i] == n->x) break;
|
||||
if (n->next == NULL)
|
||||
{n->next = malloc(sizeof(node));
|
||||
n = n->next;
|
||||
if (n == NULL) exit(EXIT_FAILURE);
|
||||
n->x = a[i];
|
||||
n->next = NULL;
|
||||
break;}}}
|
||||
|
||||
return start;}
|
||||
|
||||
int main(void)
|
||||
{int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
for (node *n = uniq(a, 10) ; n != NULL ; n = n->next)
|
||||
printf("%d ", n->x);
|
||||
puts("");
|
||||
return 0;}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Returns `true' if element `e' is in array `a'. Otherwise, returns `false'.
|
||||
* Checks only the first `n' elements. Pure, O(n).
|
||||
*/
|
||||
bool elem(int *a, size_t n, int e)
|
||||
{
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
if (a[i] == e)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Removes the duplicates in array `a' of given length `n'. Returns the number
|
||||
* of unique elements. In-place, order preserving, O(n ^ 2).
|
||||
*/
|
||||
size_t nub(int *a, size_t n)
|
||||
{
|
||||
size_t m = 0;
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
if (!elem(a, m, a[i]))
|
||||
a[m++] = a[i];
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/* Out-place version of `nub'. Pure, order preserving, alloc < n * sizeof(int)
|
||||
* bytes, O(n ^ 2).
|
||||
*/
|
||||
size_t nub_new(int **b, int *a, size_t n)
|
||||
{
|
||||
int *c = malloc(n * sizeof(int));
|
||||
memcpy(c, a, n * sizeof(int));
|
||||
int m = nub(c, n);
|
||||
*b = malloc(m * sizeof(int));
|
||||
memcpy(*b, c, m * sizeof(int));
|
||||
free(c);
|
||||
return m;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
int *b;
|
||||
|
||||
size_t n = nub_new(&b, a, sizeof(a) / sizeof(a[0]));
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
printf("%d ", b[i]);
|
||||
puts("");
|
||||
|
||||
free(b);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int icmp(const void *a, const void *b)
|
||||
{
|
||||
#define _I(x) *(int*)x
|
||||
return _I(a) < _I(b) ? -1 : _I(a) > _I(b);
|
||||
#undef _I
|
||||
}
|
||||
|
||||
/* filter items in place and return number of uniques. if a separate
|
||||
list is desired, duplicate it before calling this function */
|
||||
int uniq(int *a, int len)
|
||||
{
|
||||
int i, j;
|
||||
qsort(a, len, sizeof(int), icmp);
|
||||
for (i = j = 0; i < len; i++)
|
||||
if (a[i] != a[j]) a[++j] = a[i];
|
||||
return j + 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
int i, len = uniq(x, sizeof(x) / sizeof(x[0]));
|
||||
for (i = 0; i < len; i++) printf("%d\n", x[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
user=> (distinct [1 3 2 9 1 2 3 8 8 1 0 2])
|
||||
(1 3 2 9 8 0)
|
||||
user=>
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(remove-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
|
||||
> (9 3 8 1 0 2)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(delete-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
|
||||
> (9 3 8 1 0 2)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void main() {
|
||||
auto data = [1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2];
|
||||
data.sort();
|
||||
writeln(uniq(data));
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
auto data = [1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2];
|
||||
|
||||
int[int] hash;
|
||||
foreach (el; data)
|
||||
hash[el] = 0;
|
||||
writeln(hash.keys);
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
program RemoveDuplicateElements;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses Generics.Collections;
|
||||
|
||||
var
|
||||
i: Integer;
|
||||
lIntegerList: TList<Integer>;
|
||||
const
|
||||
INT_ARRAY: array[1..7] of Integer = (1, 2, 2, 3, 4, 5, 5);
|
||||
begin
|
||||
lIntegerList := TList<Integer>.Create;
|
||||
try
|
||||
for i in INT_ARRAY do
|
||||
if not lIntegerList.Contains(i) then
|
||||
lIntegerList.Add(i);
|
||||
|
||||
for i in lIntegerList do
|
||||
Writeln(i);
|
||||
finally
|
||||
lIntegerList.Free;
|
||||
end;
|
||||
end.
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1,2,3,2,3,4].asSet().getElements()
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
List = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5].
|
||||
UniqueList = gb_sets:to_list(gb_sets:from_list(List)).
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
include sort.e
|
||||
|
||||
function uniq(sequence s)
|
||||
sequence out
|
||||
s = sort(s)
|
||||
out = s[1..1]
|
||||
for i = 2 to length(s) do
|
||||
if not equal(s[i],out[$]) then
|
||||
out = append(out, s[i])
|
||||
end if
|
||||
end for
|
||||
return out
|
||||
end function
|
||||
|
||||
constant s = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4}
|
||||
? s
|
||||
? uniq(s)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
USING: sets ;
|
||||
V{ 1 2 1 3 2 4 5 } pruned .
|
||||
|
||||
V{ 1 2 3 4 5 }
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
\ Increments a2 until it no longer points to the same value as a1
|
||||
\ a3 is the address beyond the data a2 is traversing.
|
||||
: skip-dups ( a1 a2 a3 -- a1 a2+n )
|
||||
dup rot ?do
|
||||
over @ i @ <> if drop i leave then
|
||||
cell +loop ;
|
||||
|
||||
\ Compress an array of cells by removing adjacent duplicates
|
||||
\ Returns the new count
|
||||
: uniq ( a n -- n2 )
|
||||
over >r \ Original addr to return stack
|
||||
cells over + >r \ "to" addr now on return stack, available as r@
|
||||
dup begin ( write read )
|
||||
dup r@ <
|
||||
while
|
||||
2dup @ swap ! \ copy one cell
|
||||
cell+ r@ skip-dups
|
||||
cell 0 d+ \ increment write ptr only
|
||||
repeat r> 2drop r> - cell / ;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
: uniqv { a n \ r e -- n }
|
||||
a n cells+ to e
|
||||
a dup to r
|
||||
\ the write address lives on the stack
|
||||
begin
|
||||
r e <
|
||||
while
|
||||
r @ over !
|
||||
r cell+ e skip-dups to r
|
||||
cell+
|
||||
repeat
|
||||
a - cell / ;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
create test 1 , 2 , 3 , 2 , 6 , 4 , 5 , 3 , 6 ,
|
||||
here test - cell / constant ntest
|
||||
: .test ( n -- ) 0 ?do test i cells + ? loop ;
|
||||
|
||||
test ntest 2dup cell-sort uniq .test
|
||||
|
|
@ -0,0 +1 @@
|
|||
1 2 3 4 5 6 ok
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
program remove_dups
|
||||
implicit none
|
||||
integer :: example(12) ! The input
|
||||
integer :: res(size(example)) ! The output
|
||||
integer :: k ! The number of unique elements
|
||||
integer :: i, j
|
||||
|
||||
example = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5]
|
||||
k = 1
|
||||
res(1) = example(1)
|
||||
outer: do i=2,size(example)
|
||||
do j=1,k
|
||||
if (res(j) == example(i)) then
|
||||
! Found a match so start looking again
|
||||
cycle outer
|
||||
end if
|
||||
end do
|
||||
! No match found so add it to the output
|
||||
k = k + 1
|
||||
res(k) = example(i)
|
||||
end do outer
|
||||
write(*,advance='no',fmt='(a,i0,a)') 'Unique list has ',k,' elements: '
|
||||
write(*,*) res(1:k)
|
||||
end program remove_dups
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# Built-in, using sets (which are also lists)
|
||||
a := [ 1, 2, 3, 1, [ 4 ], 5, 5, [4], 6 ];
|
||||
# [ 1, 2, 3, 1, [ 4 ], 5, 5, [ 4 ], 6 ]
|
||||
b := Set(a);
|
||||
# [ 1, 2, 3, 5, 6, [ 4 ] ]
|
||||
IsSet(b);
|
||||
# true
|
||||
IsList(b);
|
||||
# true
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
import "fmt"
|
||||
|
||||
func uniq(list []int) []int {
|
||||
unique_set := make(map[int] bool, len(list))
|
||||
for _, x := range list {
|
||||
unique_set[x] = true
|
||||
}
|
||||
result := make([]int, len(unique_set))
|
||||
i := 0
|
||||
for x := range unique_set {
|
||||
result[i] = x
|
||||
i++
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]int {1,2,3,2,3,4})) // prints: [3 1 4 2]
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func uniq(list []int) []int {
|
||||
unique_set := make(map[int]int, len(list))
|
||||
i := 0
|
||||
for _, x := range list {
|
||||
if _, there := unique_set[x]; !there {
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
}
|
||||
result := make([]int, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func uniq(list []float64) []float64 {
|
||||
unique_set := map[float64]int{}
|
||||
i := 0
|
||||
nan := false
|
||||
for _, x := range list {
|
||||
if _, exists := unique_set[x]; exists {
|
||||
continue
|
||||
}
|
||||
if math.IsNaN(x) {
|
||||
if nan {
|
||||
continue
|
||||
} else {
|
||||
nan = true
|
||||
}
|
||||
}
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
result := make([]float64, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]float64{1, 2, math.NaN(), 2, math.NaN(), 4}))
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
def list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
assert list.size() == 12
|
||||
println " Original List: ${list}"
|
||||
|
||||
// Filtering the List
|
||||
list.unique()
|
||||
assert list.size() == 8
|
||||
println " Filtered List: ${list}"
|
||||
|
||||
list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
assert list.size() == 12
|
||||
|
||||
// Converting to Set
|
||||
def set = new HashSet(list)
|
||||
assert set.size() == 8
|
||||
println " Set: ${set}"
|
||||
|
||||
// Converting to Order-preserving Set
|
||||
set = new LinkedHashSet(list)
|
||||
assert set.size() == 8
|
||||
println "List-ordered Set: ${set}"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
values = [1,2,3,2,3,4]
|
||||
unique = List.nub values
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
REAL :: nums(12)
|
||||
CHARACTER :: workspace*100
|
||||
|
||||
nums = (1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2)
|
||||
WRITE(Text=workspace) nums ! convert to string
|
||||
EDIT(Text=workspace, SortDelDbls=workspace) ! do the job for a string
|
||||
READ(Text=workspace, ItemS=individuals) nums ! convert to numeric
|
||||
|
||||
WRITE(ClipBoard) individuals, "individuals: ", nums ! 6 individuals: 0 1 2 3 8 9 0 0 0 0 0 0
|
||||
|
|
@ -0,0 +1 @@
|
|||
non_repeated_values = array[uniq(array, sort( array))]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
procedure main(args)
|
||||
every write(!noDups(args))
|
||||
end
|
||||
|
||||
procedure noDups(L)
|
||||
every put(newL := [], notDup(set(),!L))
|
||||
return newL
|
||||
end
|
||||
|
||||
procedure notDup(cache, a)
|
||||
if not member(cache, a) then {
|
||||
insert(cache, a)
|
||||
return a
|
||||
}
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
To decide which list of Ks is (L - list of values of kind K) without duplicates:
|
||||
let result be a list of Ks;
|
||||
repeat with X running through L:
|
||||
add X to result, if absent;
|
||||
decide on result.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
~. 4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2
|
||||
4 3 2 8 0 1 9 5 7 6
|
||||
~. 'chthonic eleemosynary paronomasiac'
|
||||
chtoni elmsyarp
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
0 1 1 2 0 */0 1 2
|
||||
0 0 0
|
||||
0 1 2
|
||||
0 1 2
|
||||
0 2 4
|
||||
0 0 0
|
||||
~. 0 1 1 2 0 */0 1 2
|
||||
0 0 0
|
||||
0 1 2
|
||||
0 2 4
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Arrays;
|
||||
|
||||
Object[] data = {1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"};
|
||||
Set<Object> uniqueSet = new HashSet<Object>(Arrays.asList(data));
|
||||
Object[] unique = uniqueSet.toArray();
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function unique(ary) {
|
||||
// concat() with no args is a way to clone an array
|
||||
var u = ary.concat().sort();
|
||||
for (var i = 1; i < u.length; ) {
|
||||
if (u[i-1] === u[i])
|
||||
u.splice(i,1);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
var ary = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d", "4"];
|
||||
var uniq = unique(ary);
|
||||
for (var i = 0; i < uniq.length; i++)
|
||||
print(uniq[i] + "\t" + typeof(uniq[i]));
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
Array.prototype.unique = function() {
|
||||
var u = this.concat().sort();
|
||||
for (var i = 1; i < u.length; ) {
|
||||
if (u[i-1] === u[i])
|
||||
u.splice(i,1);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
var uniq = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"].unique();
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
a = [1,2,3,4,1,2,3,4]
|
||||
unique(a)
|
||||
31
Task/Remove-duplicate-elements/K/remove-duplicate-elements.k
Normal file
31
Task/Remove-duplicate-elements/K/remove-duplicate-elements.k
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
a:4 5#20?13 / create a random 4 x 5 matrix
|
||||
(12 7 12 4 3
|
||||
6 3 7 4 7
|
||||
3 8 3 1 2
|
||||
2 12 6 4 1)
|
||||
|
||||
,/a / flatten to array
|
||||
12 7 12 4 3 6 3 7 4 7 3 8 3 1 2 2 12 6 4 1
|
||||
|
||||
?,/a / distinct elements
|
||||
12 7 4 3 6 8 1 2
|
||||
|
||||
?"chthonic eleemosynary paronomasiac"
|
||||
"chtoni elmsyarp"
|
||||
|
||||
?("this";"that";"was";"that";"was";"this")
|
||||
("this"
|
||||
"that"
|
||||
"was")
|
||||
|
||||
0 1 1 2 0 *\: 0 1 2
|
||||
(0 0 0
|
||||
0 1 2
|
||||
0 1 2
|
||||
0 2 4
|
||||
0 0 0)
|
||||
|
||||
?0 1 1 2 0 *\: 0 1 2
|
||||
(0 0 0
|
||||
0 1 2
|
||||
0 2 4)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
: dip swap '_ set execute _ ;
|
||||
|
||||
: remove-duplicates
|
||||
[] swap do unique? length 0 == if break then loop drop ;
|
||||
: unique?
|
||||
0 extract swap "2dup in if drop else append then" dip ;
|
||||
|
||||
[1 2 6 3 6 4 5 6] remove-duplicates .
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[1 2 6 3 6 4 5 6] 's distinct
|
||||
[1 2 6 3 6 4 5 6] 's dress dup union .
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
a$ =" 1 $23.19 2 elbow 3 2 Bork 4 3 elbow 2 $23.19 "
|
||||
print "Original set of elements = ["; a$; "]"
|
||||
|
||||
b$ =removeDuplicates$( a$)
|
||||
print "With duplicates removed = ["; b$; "]"
|
||||
|
||||
end
|
||||
|
||||
function removeDuplicates$( in$)
|
||||
o$ =" "
|
||||
i =1
|
||||
do
|
||||
term$ =word$( in$, i, " ")
|
||||
if instr( o$, " "; term$; " ") =0 and term$ <>" " then o$ =o$ +term$ +" "
|
||||
i =i +1
|
||||
loop until term$ =""
|
||||
removeDuplicates$ =o$
|
||||
end function
|
||||
|
|
@ -0,0 +1 @@
|
|||
show remdup [1 2 3 a b c 2 3 4 b c d] ; [1 a 2 3 4 b c d]
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
items = {1,2,3,4,1,2,3,4,"bird","cat","dog","dog","bird"}
|
||||
flags = {}
|
||||
io.write('Unique items are:')
|
||||
for i=1,#items do
|
||||
if not flags[items[i]] then
|
||||
io.write(' ' .. items[i])
|
||||
flags[items[i]] = true
|
||||
end
|
||||
end
|
||||
io.write('\n')
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> unique([1 2 6 3 6 4 5 6])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
uniques = #(1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d")
|
||||
for i in uniques.count to 1 by -1 do
|
||||
(
|
||||
id = findItem uniques uniques[i]
|
||||
if (id != i) do deleteItem uniques i
|
||||
)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
REMDUPE(L,S)
|
||||
;L is the input listing
|
||||
;S is the separator between entries
|
||||
;R is the list to be returned
|
||||
NEW Z,I,R
|
||||
FOR I=1:1:$LENGTH(L,S) SET Z($PIECE(L,S,I))=""
|
||||
;Repack for return
|
||||
SET I="",R=""
|
||||
FOR SET I=$O(Z(I)) QUIT:I="" SET R=$SELECT($L(R)=0:I,1:R_S_I)
|
||||
KILL Z,I
|
||||
QUIT R
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> L := [ 1, 2, 1, 2, 3, 3, 2, 1, "a", "b", "b", "a", "c", "b" ];
|
||||
L := [1, 2, 1, 2, 3, 3, 2, 1, "a", "b", "b", "a", "c", "b"]
|
||||
|
||||
> [op]({op}(L));
|
||||
[1, 2, 3, "a", "b", "c"]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> convert( convert( L, 'set' ), 'list' );
|
||||
[1, 2, 3, "a", "b", "c"]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
> A := Array( L ):
|
||||
> for u in A do T[u] := 1 end: Array( [indices]( T, 'nolist' ) );
|
||||
[1, 2, 3, "c", "a", "b"]
|
||||
|
|
@ -0,0 +1 @@
|
|||
DeleteDuplicates[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]
|
||||
|
|
@ -0,0 +1 @@
|
|||
{0, 2, 1, 4, 3}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
NoDupes[input_List] := Split[Sort[input]][[All, 1]]
|
||||
NoDupes[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]
|
||||
|
|
@ -0,0 +1 @@
|
|||
{0, 1, 2, 3, 4}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
unique([8, 9, 5, 2, 0, 7, 0, 0, 4, 2, 7, 3, 9, 6, 6, 2, 4, 7, 9, 8, 3, 8, 0, 3, 7, 0, 2, 7, 6, 0]);
|
||||
[0, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using System.Linq;
|
||||
using System.Console;
|
||||
|
||||
module RemDups
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def nums = [1, 4, 6, 3, 6, 2, 7, 2, 5, 2, 6, 8];
|
||||
def unique = $[n | n in nums.Distinct()];
|
||||
WriteLine(unique);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
-- Note: Task requirement is to process "arrays". The following converts arrays into simple lists of words:
|
||||
-- Putting the resulting list back into an array is left as an exercise for the reader.
|
||||
a1 = [2, 3, 5, 7, 11, 13, 17, 19, 'cats', 222, -100.2, +11, 1.1, +7, '7.', 7, 5, 5, 3, 2, 0, 4.4, 2]
|
||||
a2 = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
a3 = ['Now', 'is', 'the', 'time', 'for', 'all', 'good', 'men', 'to', 'come', 'to', 'the', 'aid', 'of', 'the', 'party.']
|
||||
x = 0
|
||||
lists = ''
|
||||
x = x + 1; lists[0] = x; lists[x] = array2wordlist(a1)
|
||||
x = x + 1; lists[0] = x; lists[x] = array2wordlist(a2)
|
||||
x = x + 1; lists[0] = x; lists[x] = array2wordlist(a3)
|
||||
|
||||
loop ix = 1 to lists[0]
|
||||
nodups_list = remove_dups(lists[ix])
|
||||
say ix.right(4)':' lists[ix]
|
||||
say ''.right(4)':' nodups_list
|
||||
say
|
||||
end ix
|
||||
|
||||
return
|
||||
|
||||
-- =============================================================================
|
||||
method remove_dups(list) public static
|
||||
|
||||
newlist = ''
|
||||
nodups = '0'
|
||||
loop w_ = 1 to list.words()
|
||||
ix = list.word(w_)
|
||||
nodups[ix] = nodups[ix] + 1 -- we can even collect a count of dups if we want
|
||||
end w_
|
||||
loop k_ over nodups
|
||||
newlist = newlist k_
|
||||
end k_
|
||||
|
||||
return newlist.space
|
||||
|
||||
-- =============================================================================
|
||||
method array2wordlist(ra = Rexx[]) public static
|
||||
|
||||
wordlist = ''
|
||||
loop r_ over ra
|
||||
wordlist = wordlist r_
|
||||
end r_
|
||||
|
||||
return wordlist.space
|
||||
|
|
@ -0,0 +1 @@
|
|||
(unique '(1 2 3 a b c 2 3 4 b c d))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
uniques := [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
|
||||
cull uniques
|
||||
=+-+-+-+-+-+-+-+-+
|
||||
=|1|2|3|a|b|c|4|d|
|
||||
=+-+-+-+-+-+-+-+-+
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
cull 1 1 2 2 3 3
|
||||
=1 2 3
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let uniq lst =
|
||||
let unique_set = Hashtbl.create (List.length lst) in
|
||||
List.iter (fun x -> Hashtbl.replace unique_set x ()) lst;
|
||||
Hashtbl.fold (fun x () xs -> x :: xs) unique_set []
|
||||
|
||||
let _ =
|
||||
uniq [1;2;3;2;3;4]
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
use Structure;
|
||||
|
||||
bundle Default {
|
||||
class Unique {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
nums := [1, 1, 2, 3, 4, 4];
|
||||
unique := IntVector->New();
|
||||
|
||||
each(i : nums) {
|
||||
n := nums[i];
|
||||
if(unique->Has(n) = false) {
|
||||
unique->AddBack(n);
|
||||
};
|
||||
};
|
||||
|
||||
each(i : unique) {
|
||||
unique->Get(i)->PrintLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
NSArray *items = [NSArray arrayWithObjects:@"A", @"B", @"C", @"B", @"A", nil];
|
||||
|
||||
NSSet *unique = [NSSet setWithArray:items];
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
input=[1 2 6 4 2 32 5 5 4 3 3 5 1 2 32 4 4];
|
||||
output=unique(input);
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
declare
|
||||
|
||||
fun {Nub Xs}
|
||||
D = {Dictionary.new}
|
||||
in
|
||||
for X in Xs do D.X := unit end
|
||||
{Dictionary.keys D}
|
||||
end
|
||||
|
||||
in
|
||||
|
||||
{Show {Nub [1 2 1 3 5 4 3 4 4]}}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
rd(v)={
|
||||
vecsort(v,,8)
|
||||
};
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$list = array(1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd');
|
||||
$unique_list = array_unique($list);
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
declare t(20) fixed initial (1, 5, 6, 2, 1, 7,
|
||||
5, 22, 4, 19, 1, 1, 6, 6, 6, 8, 9, 10, 11, 12);
|
||||
declare (i, j, k, n, e) fixed;
|
||||
|
||||
n = hbound(t,1);
|
||||
i = 0;
|
||||
outer:
|
||||
do k = 1 to n;
|
||||
e = t(k);
|
||||
do j = k-1 to 1 by -1;
|
||||
if e = t(j) then iterate outer;
|
||||
end;
|
||||
i = i + 1;
|
||||
t(i) = e;
|
||||
end;
|
||||
|
||||
put skip list ('Unique elements are:');
|
||||
put edit ((t(k) do k = 1 to i)) (skip, f(11));
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
Program RemoveDuplicates;
|
||||
|
||||
const
|
||||
iArray: array[1..7] of integer = (1, 2, 2, 3, 4, 5, 5);
|
||||
|
||||
var
|
||||
rArray: array[1..7] of integer;
|
||||
i, pos, last: integer;
|
||||
newNumber: boolean;
|
||||
|
||||
begin
|
||||
rArray[1] := iArray[1];
|
||||
last := 1;
|
||||
pos := 1;
|
||||
while pos < high(iArray) do
|
||||
begin
|
||||
inc(pos);
|
||||
newNumber := true;
|
||||
for i := low(rArray) to last do
|
||||
if iArray[pos] = rArray[i] then
|
||||
begin
|
||||
newNumber := false;
|
||||
break;
|
||||
end;
|
||||
if newNumber then
|
||||
begin
|
||||
inc(last);
|
||||
rArray[last] := iArray[pos];
|
||||
end;
|
||||
end;
|
||||
for i := low(rArray) to last do
|
||||
writeln (rArray[i]);
|
||||
end.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
sub nub (@a) {
|
||||
my @b;
|
||||
none(@b) eqv $_ and push @b, $_ for @a;
|
||||
return @b;
|
||||
}
|
||||
|
||||
my @unique = nub [1, 2, 3, 5, 2, 4, 3, -3, 7, 5, 6];
|
||||
|
|
@ -0,0 +1 @@
|
|||
my @unique = [1, 2, 3, 5, 2, 4, 3, -3, 7, 5, 6].uniq;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
use List::MoreUtils qw(uniq);
|
||||
|
||||
my @uniq = uniq qw(1 2 3 a b c 2 3 4 b c d);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my %seen;
|
||||
my @uniq = grep {!$seen{$_}++} qw(1 2 3 a b c 2 3 4 b c d);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my %hash = map { $_ => 1 } qw(1 2 3 a b c 2 3 4 b c d);
|
||||
my @uniq = keys %hash;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
my %seen;
|
||||
@seen{qw(1 2 3 a b c 2 3 4 b c d)} = ();
|
||||
my @uniq = keys %seen;
|
||||
|
|
@ -0,0 +1 @@
|
|||
(uniq (2 4 6 1 2 3 4 5 6 1 3 5))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
;;; Initial array
|
||||
lvars ar = {1 2 3 2 3 4};
|
||||
;;; Create a hash table
|
||||
lvars ht= newmapping([], 50, 0, true);
|
||||
;;; Put all array as keys into the hash table
|
||||
lvars i;
|
||||
for i from 1 to length(ar) do
|
||||
1 -> ht(ar(i))
|
||||
endfor;
|
||||
|
||||
;;; Collect keys into a list
|
||||
lvars ls = [];
|
||||
appdata(ht, procedure(x); cons(front(x), ls) -> ls; endprocedure);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[10 8 8 98 32 2 4 5 10 ] dup length dict begin aload let* currentdict {pop} map end
|
||||
|
|
@ -0,0 +1 @@
|
|||
$data = 1,2,3,1,2,3,4,1
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
$h = @{}
|
||||
foreach ($x in $data) {
|
||||
$h[$x] = 1
|
||||
}
|
||||
$h.Keys
|
||||
|
|
@ -0,0 +1 @@
|
|||
$data | Sort-Object -Unique
|
||||
|
|
@ -0,0 +1 @@
|
|||
$data | Select-Object -Unique
|
||||
|
|
@ -0,0 +1 @@
|
|||
uniq(Data,Uniques) :- sort(Data,Uniques).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
?- uniq([1, 2, 3, 2, 3, 4],Xs).
|
||||
Xs = [1, 2, 3, 4]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
member1(X,[H|_]) :- X==H,!.
|
||||
member1(X,[_|T]) :- member1_(X,T).
|
||||
|
||||
distinct([],[]).
|
||||
distinct([H|T],C) :- member1(H,T),!, distinct(T,C).
|
||||
distinct([H|T],[H|C]) :- distinct(T,C).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
?- distinct([A, A, 1, 2, 3, 2, 3, 4],Xs).
|
||||
Xs = [A, 1, 2, 3, 4]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
NewMap MyElements.s()
|
||||
|
||||
For i=0 To 9 ;Mark 10 items at random, causing high risk of duplication items.
|
||||
x=Random(9)
|
||||
t$="Number "+str(x)+" is marked"
|
||||
MyElements(str(x))=t$ ; Add element 'X' to the hash list or overwrite if already included.
|
||||
Next
|
||||
|
||||
ForEach MyElements()
|
||||
Debug MyElements()
|
||||
Next
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue