WeixiangYan/CodeTransOcean huggingface dataset:
Task: Active object








VB: Module Module1
 
    Sub Main()
        Using active As New Integrator
            active.Operation = Function(t As Double) Math.Sin(2 * Math.PI * 0.5 * t)
            Threading.Thread.Sleep(TimeSpan.FromSeconds(2))
            Console.WriteLine(active.Value)
            active.Operation = Function(t As Double) 0
            Threading.Thread.Sleep(TimeSpan.FromSeconds(0.5))
            Console.WriteLine(active.Value)
        End Using
        Console.ReadLine()
    End Sub
 
End Module
  
Class Integrator
    Implements IDisposable
 
    Private m_Operation As Func(Of Double, Double)
    Private m_Disposed As Boolean
    Private m_SyncRoot As New Object
    Private m_Value As Double
 
    Public Sub New()
        m_Operation = Function(void) 0.0
        Dim t As New Threading.Thread(AddressOf MainLoop)
        t.Start()
    End Sub
 
    Private Sub MainLoop()
        Dim epoch = Now
        Dim t0 = 0.0
        Do
            SyncLock m_SyncRoot
                Dim t1 = (Now - epoch).TotalSeconds
                m_Value = m_Value + (Operation(t1) + Operation(t0)) * (t1 - t0) / 2
                t0 = t1
            End SyncLock
            Threading.Thread.Sleep(10)
        Loop Until m_Disposed
    End Sub
 
    Public Property Operation() As Func(Of Double, Double)
        Get
            SyncLock m_SyncRoot
                Return m_Operation
            End SyncLock
        End Get
        Set(ByVal value As Func(Of Double, Double))
            SyncLock m_SyncRoot
                m_Operation = value
            End SyncLock
        End Set
    End Property
 
    Public ReadOnly Property Value() As Double
        Get
            SyncLock m_SyncRoot
                Return m_Value
            End SyncLock
        End Get
    End Property
 
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        m_Disposed = True
    End Sub
 
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
 
End Class
Task: Active object




Go: package main

import (
    "fmt"
    "math"
    "time"
)



type tFunc func(time.Duration) float64



type aio struct {
    iCh chan tFunc        
    oCh chan chan float64 
}


func newAio() *aio {
    var a aio
    a.iCh = make(chan tFunc)
    a.oCh = make(chan chan float64)
    go aif(&a)
    return &a
}




func (a aio) input(f tFunc) {
    a.iCh <- f
}




func (a aio) output() float64 {
    sCh := make(chan float64)
    a.oCh <- sCh
    return <-sCh
}


func zeroFunc(time.Duration) float64 { return 0 }


func aif(a *aio) {
    var k tFunc = zeroFunc 
    s := 0.                
    t0 := time.Now()       
    k0 := k(0)             
    t1 := t0               
    k1 := k0

    tk := time.Tick(10 * time.Millisecond) 
    for {
        select {
        case t2 := <-tk: 
            k2 := k(t2.Sub(t0))                        
            s += (k1 + k2) * .5 * t2.Sub(t1).Seconds() 
            t1, k1 = t2, k2                            
        case k = <-a.iCh: 
        case sCh := <-a.oCh: 
            sCh <- s
        }
    }
}

func main() {
    a := newAio()                           
    a.input(func(t time.Duration) float64 { 
        return math.Sin(t.Seconds() * math.Pi)
    })
    time.Sleep(2 * time.Second) 
    a.input(zeroFunc)           
    time.Sleep(time.Second / 2) 
    fmt.Println(a.output())     
}
Task: Twelve statements



C#: using System;
using System.Collections.Generic;
using System.Linq;
    
public static class TwelveStatements
{
    public static void Main() {
        Func<Statements, bool>[] checks = {
            st => st[1],
            st => st[2] == (7.To(12).Count(i => st[i]) == 3),
            st => st[3] == (2.To(12, by: 2).Count(i => st[i]) == 2),
            st => st[4] == st[5].Implies(st[6] && st[7]),
            st => st[5] == (!st[2] && !st[3] && !st[4]),
            st => st[6] == (1.To(12, by: 2).Count(i => st[i]) == 4),
            st => st[7] == (st[2] != st[3]),
            st => st[8] == st[7].Implies(st[5] && st[6]),
            st => st[9] == (1.To(6).Count(i => st[i]) == 3),
            st => st[10] == (st[11] && st[12]),
            st => st[11] == (7.To(9).Count(i => st[i]) == 1),
            st => st[12] == (1.To(11).Count(i => st[i]) == 4)
        };
        
        for (Statements statements = new Statements(0); statements.Value < 4096; statements++) {
            int count = 0;
            int falseIndex = 0;
            for (int i = 0; i < checks.Length; i++) {
                if (checks[i](statements)) count++;
                else falseIndex = i;
            }
            if (count == 0) Console.WriteLine($"{"All wrong:", -13}{statements}");
            else if (count == 11) Console.WriteLine($"{$"Wrong at {falseIndex + 1}:", -13}{statements}");
            else if (count == 12) Console.WriteLine($"{"All correct:", -13}{statements}");
        }
    }
    
    struct Statements
    {    
        public Statements(int value) : this() { Value = value; }
        
        public int Value { get; }
                
        public bool this[int index] => (Value & (1 << index - 1)) != 0;
        
        public static Statements operator ++(Statements statements) => new Statements(statements.Value + 1);
        
        public override string ToString() {
            Statements copy = this; 
            return string.Join(" ", from i in 1.To(12) select copy[i] ? "T" : "F");
        }
        
    }
    
    
    static bool Implies(this bool x, bool y) => !x || y;
    
    static IEnumerable<int> To(this int start, int end, int by = 1) {
        while (start <= end) {
            yield return start;
            start += by;
        }
    }

}
Task: Twelve statements





Java: public class LogicPuzzle
{
    boolean S[] = new boolean[13];
    int Count = 0;

    public boolean check2 ()
    {
        int count = 0;
        for (int k = 7; k <= 12; k++)
            if (S[k]) count++;
        return S[2] == (count == 3);
    }

    public boolean check3 ()
    {
        int count = 0;
        for (int k = 2; k <= 12; k += 2)
            if (S[k]) count++;
        return S[3] == (count == 2);
    }

    public boolean check4 ()
    {
        return S[4] == ( !S[5] || S[6] && S[7]);
    }

    public boolean check5 ()
    {
        return S[5] == ( !S[2] && !S[3] && !S[4]);
    }

    public boolean check6 ()
    {
        int count = 0;
        for (int k = 1; k <= 11; k += 2)
            if (S[k]) count++;
        return S[6] == (count == 4);
    }

    public boolean check7 ()
    {
        return S[7] == ((S[2] || S[3]) && !(S[2] && S[3]));
    }

    public boolean check8 ()
    {
        return S[8] == ( !S[7] || S[5] && S[6]);
    }

    public boolean check9 ()
    {
        int count = 0;
        for (int k = 1; k <= 6; k++)
            if (S[k]) count++;
        return S[9] == (count == 3);
    }

    public boolean check10 ()
    {
        return S[10] == (S[11] && S[12]);
    }

    public boolean check11 ()
    {
        int count = 0;
        for (int k = 7; k <= 9; k++)
            if (S[k]) count++;
        return S[11] == (count == 1);
    }

    public boolean check12 ()
    {
        int count = 0;
        for (int k = 1; k <= 11; k++)
            if (S[k]) count++;
        return S[12] == (count == 4);
    }

    public void check ()
    {
        if (check2() && check3() && check4() && check5() && check6()
            && check7() && check8() && check9() && check10() && check11()
            && check12())
        {
            for (int k = 1; k <= 12; k++)
                if (S[k]) System.out.print(k + " ");
            System.out.println();
            Count++;
        }
    }

    public void recurseAll (int k)
    {
        if (k == 13)
            check();
        else
        {
            S[k] = false;
            recurseAll(k + 1);
            S[k] = true;
            recurseAll(k + 1);
        }
    }

    public static void main (String args[])
    {
        LogicPuzzle P = new LogicPuzzle();
        P.S[1] = true;
        P.recurseAll(2);
        System.out.println();
        System.out.println(P.Count + " Solutions found.");
    }
}
Task: Twelve statements







Python: from itertools import product


constraintinfo = (  
  (lambda st: len(st) == 12                 ,(1, 'This is a numbered list of twelve statements')),
  (lambda st: sum(st[-6:]) == 3             ,(2, 'Exactly 3 of the last 6 statements are true')),
  (lambda st: sum(st[1::2]) == 2            ,(3, 'Exactly 2 of the even-numbered statements are true')),
  (lambda st: (st[5]&st[6]) if st[4] else 1 ,(4, 'If statement 5 is true, then statements 6 and 7 are both true')),
  (lambda st: sum(st[1:4]) == 0             ,(5, 'The 3 preceding statements are all false')),
  (lambda st: sum(st[0::2]) == 4            ,(6, 'Exactly 4 of the odd-numbered statements are true')),
  (lambda st: sum(st[1:3]) == 1             ,(7, 'Either statement 2 or 3 is true, but not both')),
  (lambda st: (st[4]&st[5]) if st[6] else 1 ,(8, 'If statement 7 is true, then 5 and 6 are both true')),
  (lambda st: sum(st[:6]) == 3              ,(9, 'Exactly 3 of the first 6 statements are true')),
  (lambda st: (st[10]&st[11])               ,(10, 'The next two statements are both true')),
  (lambda st: sum(st[6:9]) == 1             ,(11, 'Exactly 1 of statements 7, 8 and 9 are true')),
  (lambda st: sum(st[0:11]) == 4            ,(12, 'Exactly 4 of the preceding statements are true')),
)  

def printer(st, matches):
    if False in matches:
        print('Missed by one statement: %i, %s' % docs[matches.index(False)])
    else:
        print('Full match:')
    print('  ' + ', '.join('%i:%s' % (i, 'T' if t else 'F') for i, t in enumerate(st, 1)))

funcs, docs = zip(*constraintinfo)

full, partial = [], []

for st in product( *([(False, True)] * 12) ):
    truths = [bool(func(st)) for func in funcs]
    matches = [s == t for s,t in zip(st, truths)]
    mcount = sum(matches)
    if mcount == 12:
        full.append((st, matches))
    elif mcount == 11:
        partial.append((st, matches))

for stm in full + partial:
    printer(*stm)
Task: Twelve statements








VB: Public s As String    
Public t As Integer   
 
Function s1()
    s1 = Len(s) = 12
End Function
Function s2()
    t = 0
    For i = 7 To 12
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s2 = t = 3
End Function
Function s3()
    t = 0
    For i = 2 To 12 Step 2
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s3 = t = 2
End Function
Function s4()
    s4 = Mid(s, 5, 1) = "0" Or ((Mid(s, 6, 1) = "1" And Mid(s, 7, 1) = "1"))
End Function
Function s5()
    s5 = Mid(s, 2, 1) = "0" And Mid(s, 3, 1) = "0" And Mid(s, 4, 1) = "0"
End Function
Function s6()
    t = 0
    For i = 1 To 12 Step 2
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s6 = t = 4
End Function
Function s7()
    s7 = Mid(s, 2, 1) <> Mid(s, 3, 1)
End Function
Function s8()
    s8 = Mid(s, 7, 1) = "0" Or (Mid(s, 5, 1) = "1" And Mid(s, 6, 1) = "1")
End Function
Function s9()
    t = 0
    For i = 1 To 6
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s9 = t = 3
End Function
Function s10()
    s10 = Mid(s, 11, 1) = "1" And Mid(s, 12, 1) = "1"
End Function
Function s11()
    t = 0
    For i = 7 To 9
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s11 = t = 1
End Function
Function s12()
    t = 0
    For i = 1 To 11
        t = t - (Mid(s, i, 1) = "1")
    Next i
    s12 = t = 4
End Function
 
Public Sub twelve_statements()
    For i = 0 To 2 ^ 12 - 1
        s = Right(CStr(WorksheetFunction.Dec2Bin(64 + i \ 128)), 5) _
            & Right(CStr(WorksheetFunction.Dec2Bin(256 + i Mod 128)), 7)
        For b = 1 To 12
            Select Case b
                Case 1: If s1 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 2: If s2 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 3: If s3 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 4: If s4 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 5: If s5 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 6: If s6 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 7: If s7 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 8: If s8 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 9: If s9 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 10: If s10 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 11: If s11 <> (Mid(s, b, 1) = "1") Then Exit For
                Case 12: If s12 <> (Mid(s, b, 1) = "1") Then Exit For
            End Select
            If b = 12 Then Debug.Print s
        Next
    Next
End Sub
Task: Twelve statements




Go: package main

import "fmt"


var solution = make(chan int)
var nearMiss = make(chan int)
var done = make(chan bool)

func main() {
    
    for i := 0; i < 4096; i++ {
        go checkPerm(i)
    }
    
    var ms []int
    for i := 0; i < 4096; {
        select {
        case <-done:
            i++
        case s := <-solution:
            print12("solution", s)
        case m := <-nearMiss:
            ms = append(ms, m)
        }
    }
    for _, m := range ms {
        print12("near miss", m)
    }
}

func print12(label string, bits int) {
    fmt.Print(label, ":")
    for i := 1; i <= 12; i++ {
        if bits&1 == 1 {
            fmt.Print(" ", i)
        }
        bits >>= 1
    }
    fmt.Println()
}

func checkPerm(tz int) {
    
    
    ts := func(n uint) bool {
        return tz>>(n-1)&1 == 1
    }
    
    
    ntrue := func(xs ...uint) int {
        nt := 0
        for _, x := range xs {
            if ts(x) {
                nt++
            }
        }
        return nt
    }
    
    
    
    
    var con bool
    
    test := func(statement uint, b bool) {
        switch {
        case ts(statement) == b:
        case con:
            panic("bail")
        default:
            con = true
        }
    }
    
    defer func() {
        if x := recover(); x != nil {
            if msg, ok := x.(string); !ok && msg != "bail" {
                panic(x)
            }
        }
        done <- true
    }()

    
    test(1, true)

    
    test(2, ntrue(7, 8, 9, 10, 11, 12) == 3)

    
    test(3, ntrue(2, 4, 6, 8, 10, 12) == 2)

    
    test(4, !ts(5) || ts(6) && ts(7))

    
    test(5, !ts(4) && !ts(3) && !ts(2))

    
    test(6, ntrue(1, 3, 5, 7, 9, 11) == 4)

    
    test(7, ts(2) != ts(3))

    
    test(8, !ts(7) || ts(5) && ts(6))

    
    test(9, ntrue(1, 2, 3, 4, 5, 6) == 3)
    
    
    test(10, ts(11) && ts(12))
    
    
    test(11, ntrue(7, 8, 9) == 1)
    
    
    test(12, ntrue(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) == 4)

    
    if con {
        nearMiss <- tz
    } else {
        solution <- tz
    }
}
Task: Remove duplicate elements



C#: 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);









Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements

C: #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;}











Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements


C++: #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;
}










Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements




Go: 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, 0, len(unique_set))
	for x := range unique_set {
		result = append(result, x)
	}
	return result
}

func main() {
	fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) 
}








Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements





Java: module RetainUniqueValues
    {
    @Inject Console console;
    void run()
        {
        Int[] array = [1, 2, 3, 2, 1, 2, 3, 4, 5, 3, 2, 1];
        array = array.distinct().toArray();
        console.print($"result={array}");
        }
    }







Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements







Python: items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = list(set(items))





Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements








VB: Option Explicit

Sub Main()
Dim myArr() As Variant, i As Long

    myArr = Remove_Duplicate(Array(1.23456789101112E+16, True, False, True, "Alpha", 1, 235, 4, 1.25, 1.25, "Beta", 1.23456789101112E+16, "Delta", "Alpha", "Charlie", 1, 2, "Foxtrot", "Foxtrot", "Alpha", 235))

    For i = LBound(myArr) To UBound(myArr)
        Debug.Print myArr(i)
    Next
End Sub

Private Function Remove_Duplicate(Arr As Variant) As Variant()
Dim myColl As New Collection, Temp() As Variant, i As Long, cpt As Long

    ReDim Temp(UBound(Arr))
    For i = LBound(Arr) To UBound(Arr)
        On Error Resume Next
        myColl.Add CStr(Arr(i)), CStr(Arr(i))
        If Err.Number > 0 Then
            On Error GoTo 0
        Else
            Temp(cpt) = Arr(i)
            cpt = cpt + 1
        End If
    Next i
    ReDim Preserve Temp(cpt - 1)
    Remove_Duplicate = Temp
End Function




Ada: with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
 
procedure Duplicate is	
	package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
	Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
	Unique : Int_Sets.Set;
begin
	for n of Nums loop
		Unique.Include (n);
	end loop;
	for e of Unique loop
		Put (e'img);
	end loop;
end Duplicate;
Task: Remove duplicate elements

C: #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;}
Task: Remove duplicate elements



C#: 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);
Task: Remove duplicate elements


C++: #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;
}
Task: Remove duplicate elements





Java: module RetainUniqueValues
    {
    @Inject Console console;
    void run()
        {
        Int[] array = [1, 2, 3, 2, 1, 2, 3, 4, 5, 3, 2, 1];
        array = array.distinct().toArray();
        console.print($"result={array}");
        }
    }
Task: Remove duplicate elements







Python: items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = list(set(items))
Task: Remove duplicate elements








VB: Option Explicit

Sub Main()
Dim myArr() As Variant, i As Long

    myArr = Remove_Duplicate(Array(1.23456789101112E+16, True, False, True, "Alpha", 1, 235, 4, 1.25, 1.25, "Beta", 1.23456789101112E+16, "Delta", "Alpha", "Charlie", 1, 2, "Foxtrot", "Foxtrot", "Alpha", 235))

    For i = LBound(myArr) To UBound(myArr)
        Debug.Print myArr(i)
    Next
End Sub

Private Function Remove_Duplicate(Arr As Variant) As Variant()
Dim myColl As New Collection, Temp() As Variant, i As Long, cpt As Long

    ReDim Temp(UBound(Arr))
    For i = LBound(Arr) To UBound(Arr)
        On Error Resume Next
        myColl.Add CStr(Arr(i)), CStr(Arr(i))
        If Err.Number > 0 Then
            On Error GoTo 0
        Else
            Temp(cpt) = Arr(i)
            cpt = cpt + 1
        End If
    Next i
    ReDim Preserve Temp(cpt - 1)
    Remove_Duplicate = Temp
End Function
Task: Remove duplicate elements




Go: 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, 0, len(unique_set))
	for x := range unique_set {
		result = append(result, x)
	}
	return result
}

func main() {
	fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) 
}