AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to get unique id from array (https://forums.alliedmods.net/showthread.php?t=337931)

Mikka 05-27-2022 04:12

How to get unique id from array
 
1 Attachment(s)
Hello everyone, i have no idea to get unique id from Array (maybe loop for etc?

+ARUKARI- 05-27-2022 04:45

Re: How to get unique id from array
 
Non tested.
PHP Code:

stock array_unique(array[], size)
{
    for (new 
0size 1; ++i) {
        for (new 
1size; ++j) {
            if (array[
i] == array[j]) {
                
copy(array[1], size 1, array[i]);
                --
size;
                --
j;
            }
        }
    }

    return 
size;



HamletEagle 05-27-2022 05:39

Re: How to get unique id from array
 
If you always have the ids in sorted order like in your example, then the solution can be done more efficiently(one pass over 2 or O(n) vs O(n^2)) compared to ARUKARI's solution.

PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
currentValue = -1;
    
    for(new 
0numElementsi++)
    {
        if(
arr[i] != currentValue)
        {
            if(
currentValue != -1)
            {
                
output[numUnique++] = currentValue;
            }
           
            
currentValue arr[i];
        }
    }
    
    
output[numUnique++] = currentValue;


My code doesn't change the original array, it extracts the data into a new one, but the solution can be modified to work in place. I leave this to you, as practice.
Note that I assumed your ids are >0. The code will not work correctly if some ids are -1. You also need to handle the case when the array is empty(just check before doing anything).

If the ids are not sorted, then you can still do it in only one pass(O(n)), by using a trie(assuming the trie implementation inside AMXX provides O(1) access on average). If it is a tree-based implementation, then likely it provides O(log(n)) access which results in an O(n * log(n)) overall complexity, which is still better than O(n^2).
Keep in mind that this matters only when the size of your array grows. If your array is small, then any solution will do just fine.

Below is the solution using a trie:

PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
Trie:set TrieCreate();
    new 
strKey[12];
    
    for(new 
0numElementsi++)
    {
        
num_to_str(input[i], strKeycharsmax(strKey));
        if(!
TrieKeyExists(setstrKey))
        {
            
output[numUnique++] = arr[i];
            
TriePushCell(setstrKey0);
        }
    }
    
    
TrieDestroy(set);


Here is how you can call either of these 2 functions:
PHP Code:

//assuming you have an array ids you want to process with numIds ids inside it

new uniqueIds[SIZE_HERE]
int numUnique;
getUniqueElements(idsnumIdsuniqueIdsnumUnique)

for(
int i 0numUniquei++)
{
    
//do something with uniqueIds[i]



Mikka 05-27-2022 07:14

Re: How to get unique id from array
 
Its still not working in my mind.
When record == 1 its working. But records > 1 motd will be broken


There my code:

+ARUKARI- 05-27-2022 07:44

Re: How to get unique id from array
 
I don't know how jackpotSkin is structured.
I think the size is different.

PHP Code:

size array_unique(jackpotSkin[JACKPOT_OWNER], sizeof(jackpotSkin[JACKPOT_OWNER])); 

Also, HamletEagle's method looks better than mine.

Mikka 05-27-2022 08:08

Re: How to get unique id from array
 
Thanks bro
Your function not working because always showing same member

Mikka 05-27-2022 08:45

Re: How to get unique id from array
 
Your statements are too complicated for me

Quote:

Originally Posted by HamletEagle (Post 2780235)
If you always have the ids in sorted order like in your example, then the solution can be done more efficiently(one pass over 2 or O(n) vs O(n^2)) compared to ARUKARI's solution.

PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
currentValue = -1;
    
    for(new 
0numElementsi++)
    {
        if(
arr[i] != currentValue)
        {
            if(
currentValue != -1)
            {
                
output[numUnique++] = currentValue;
            }
           
            
currentValue arr[i];
        }
    }
    
    
output[numUnique++] = currentValue;


My code doesn't change the original array, it extracts the data into a new one, but the solution can be modified to work in place. I leave this to you, as practice.
Note that I assumed your ids are >0. The code will not work correctly if some ids are -1. You also need to handle the case when the array is empty(just check before doing anything).

If the ids are not sorted, then you can still do it in only one pass(O(n)), by using a trie(assuming the trie implementation inside AMXX provides O(1) access on average). If it is a tree-based implementation, then likely it provides O(log(n)) access which results in an O(n * log(n)) overall complexity, which is still better than O(n^2).
Keep in mind that this matters only when the size of your array grows. If your array is small, then any solution will do just fine.

Below is the solution using a trie:

PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
Trie:set TrieCreate();
    new 
strKey[12];
    
    for(new 
0numElementsi++)
    {
        
num_to_str(input[i], strKeycharsmax(strKey));
        if(!
TrieKeyExists(setstrKey))
        {
            
output[numUnique++] = arr[i];
            
TriePushCell(setstrKey0);
        }
    }
    
    
TrieDestroy(set);


Here is how you can call either of these 2 functions:
PHP Code:

//assuming you have an array ids you want to process with numIds ids inside it

new uniqueIds[SIZE_HERE]
int numUnique;
getUniqueElements(idsnumIdsuniqueIdsnumUnique)

for(
int i 0numUniquei++)
{
    
//do something with uniqueIds[i]




HamletEagle 05-27-2022 09:09

Re: How to get unique id from array
 
Okay, use this:
PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
Trie:set TrieCreate();
    new 
strKey[12];
    
    for(new 
0numElementsi++)
    {
        
num_to_str(input[i], strKeycharsmax(strKey));
        if(!
TrieKeyExists(setstrKey))
        {
            
output[numUnique++] = arr[i];
            
TriePushCell(setstrKey0);
        }
    }
    
    
TrieDestroy(set);


Call it like this:

PHP Code:

//assuming you have an array ids you want to process with numIds ids inside it

new uniqueIds[SIZE_HERE]
int numUnique;
getUniqueElements(idsnumIdsuniqueIdsnumUnique)

for(
int i 0numUniquei++)
{
    
//do something with uniqueIds[i]


That's all you need to know to use it. Everything else was just extra information and explanations about why I suggested both solutions.

Mikka 05-27-2022 09:13

Re: How to get unique id from array
 
is this stock to be called inside a function with motd?

Quote:

Originally Posted by HamletEagle (Post 2780255)
Okay, use this:
PHP Code:

getUniqueElements(inputnumElementsoutput, &numUnique)
{
    new 
Trie:set TrieCreate();
    new 
strKey[12];
    
    for(new 
0numElementsi++)
    {
        
num_to_str(input[i], strKeycharsmax(strKey));
        if(!
TrieKeyExists(setstrKey))
        {
            
output[numUnique++] = arr[i];
            
TriePushCell(setstrKey0);
        }
    }
    
    
TrieDestroy(set);




HamletEagle 05-27-2022 09:28

Re: How to get unique id from array
 
You need to call this to do what you asked in your original post: get unique ids from an array.


All times are GMT -4. The time now is 21:19.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.