Raised This Month: $51 Target: $400
 12% 

How to get unique id from array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mikka
Member
Join Date: Dec 2018
Old 05-27-2022 , 04:12   How to get unique id from array
Reply With Quote #1

Hello everyone, i have no idea to get unique id from Array (maybe loop for etc?
Attached Thumbnails
Click image for larger version

Name:	1euVXMn.png
Views:	17
Size:	4.7 KB
ID:	195180  
Mikka is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 05-27-2022 , 04:45   Re: How to get unique id from array
Reply With Quote #2

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;

__________________
GitHub
SteamWishlist

六四天安門事件
+ARUKARI- is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-27-2022 , 05:39   Re: How to get unique id from array
Reply With Quote #3

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]

__________________

Last edited by HamletEagle; 05-27-2022 at 05:42.
HamletEagle is online now
Mikka
Member
Join Date: Dec 2018
Old 05-27-2022 , 07:14   Re: How to get unique id from array
Reply With Quote #4

Its still not working in my mind.
When record == 1 its working. But records > 1 motd will be broken


There my code:

Last edited by Mikka; 05-27-2022 at 09:09.
Mikka is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 05-27-2022 , 07:44   Re: How to get unique id from array
Reply With Quote #5

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.
__________________
GitHub
SteamWishlist

六四天安門事件
+ARUKARI- is offline
Mikka
Member
Join Date: Dec 2018
Old 05-27-2022 , 08:08   Re: How to get unique id from array
Reply With Quote #6

Thanks bro
Your function not working because always showing same member
Mikka is offline
Mikka
Member
Join Date: Dec 2018
Old 05-27-2022 , 08:45   Re: How to get unique id from array
Reply With Quote #7

Your statements are too complicated for me

Quote:
Originally Posted by HamletEagle View Post
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 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-27-2022 , 09:09   Re: How to get unique id from array
Reply With Quote #8

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.
__________________

Last edited by HamletEagle; 05-27-2022 at 09:09.
HamletEagle is online now
Mikka
Member
Join Date: Dec 2018
Old 05-27-2022 , 09:13   Re: How to get unique id from array
Reply With Quote #9

is this stock to be called inside a function with motd?

Quote:
Originally Posted by HamletEagle View Post
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);

Mikka is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-27-2022 , 09:28   Re: How to get unique id from array
Reply With Quote #10

You need to call this to do what you asked in your original post: get unique ids from an array.
__________________
HamletEagle is online now
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:27.


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