Finding duplicates in array

I have searched the internets but cannot find an example of a script to find duplicates in a single dimension array. I see there may be a count or count-only statement but have not been able to figure it out. I create an array of values, some of these values may be duplicates and I would like to find them. The actual script will be to create an array of Agent Remote ID’s, find the duplicates send an email and/or add the IP’s to an address-list for other action.

Thanks.

It’s easy

Take first element of array A, compare with all the other elements on temporary array B,
if not already exist the same on temporary array B, copy on temporary array B,
repeat for each record of array A.

At the end copy the temporary array B over array A

Done.

search tag # rextended remove duplicates on array


wow, how many?


{
:local arrA {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:local arrB [:toarray “”]

:put $arrA

:foreach itemA in=$arrA do={
:local chk true
:foreach itemB in=$arrB do={:if ($itemA = $itemB) do={:set chk false}}
:if ($chk) do={:set arrB ($arrB,$itemA)}
}
:set arrA $arrB

:put $arrA
}
:global remduponarray do={
:local retarr [:toarray “”]
:foreach srcitem in=$1 do={
:local copy true
:foreach dstitem in=$retarr do={:if ($srcitem = $dstitem) do={:set copy false}}
:if ($copy) do={:set retarr ($retarr,$srcitem)}
}
:return $retarr
}

example:

{
:local arraywithdup {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:put [$remduponarray $arraywithdup]

}

Thanks, here is what I came up with:

:local ari {1;1;2;3;4;5;4};
:local aritmp {“”};
:local aridups {“”};

:log info “Array is:”;
:log info $ari;

:local values {10;20;20;20;30;40}
:foreach v in=$ari do={
:local p [:find $aritmp $v]
:if ([:type $p]=“nil”) do={
:log info “$v is not in array”
:set aritmp ($aritmp, $v)
} else={
:log info “$v is in array”
:set aridups ($aridups, $v)
}
}
:log info “Duplicates:”;
:log info $aridups;

So, do you want to know which and how many duplicates are in the array?

This returns an array with just the duplicate values and how many times they are duplicated.
:global countduponarray do={
:local retarr [:toarray “”]
:foreach srcitem in=$1 do={
:local equal false
:foreach dstitem in=$1 do={:if ($srcitem = $dstitem) do={:set equal true}}
:if ($equal) do={:set ($retarr->“$[:tostr $srcitem]”) ([:tonum ($retarr->“$[:tostr $srcitem]”)] +1)}
}
:return $retarr
}

example:

{
:local arraywithdup {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:put [$countduponarray $arraywithdup]

}
return this array:

0=2;1=4;13=2;144=2;2=2;21=2;3=2;34=2;5=2;55=2;8=2;89=2

internetS (joke)

:wink:


Let me know if the functions is what you need at the end.

A bit off topic but pertains to what I am trying to accomplish. I swear I used this command yesterday but now get a syntax error.

:foreach i in=[find server=“NOC3-CGNAT”] do={ :set array ($array, [get $i agent-remote-id])}

I get a syntax error at “array”.

I do not see the rest of the script,
“array” is already defined globally or locally?
(is better not use some words, like array, number, string, etc. …)

Agreed, I was just doing some quick tests and debugging. array was defined as local array {"}. I got it working, not really sure how. Working with Mikrotik scripts is a bit difficult. The console will show errors in the syntax, winbox script box does not.

I added the Mikrotik script for Visual Studio on my Mac. Only problem I have now is that I have to use Wine to use winbox64.exe and after a few iterations copy and paste no longer work until I close and open all windows.