Jump to content

Would anyone be able to illustrate how i would merge data in to an additional field of an array if there were duplicates of a name.

eg.

Data:

 

Name:             PhoneNumber:

James              12345

William              45678

James               56789

Connor              88765

Peter                22468

 

Where in this situation James would have two numbers to his name.

Outcome:

 

Name:             PhoneNumber:

James              12345

                         56789

William             45678               

Connor             88765

Peter                22468

 

im not sure logically how i would do this

My Rig:

PS4

 

Link to comment
https://linustechtips.com/topic/488814-duplicate-elements-in-array/
Share on other sites

Link to post
Share on other sites

If they would be two different people, use ID numbers to separate them. If they're the same person, separate the numbers with commas which you can use to separate the numbers later.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to post
Share on other sites

Iterate over the array and see if any of the elements are the same (so compare every element to the first element, then every element to the second element, etc...). Of course, this would be easier if you could implement a sort/place properly (but that would only apply if you're building the array and not given the array in the first place. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

If they would be two different people, use ID numbers to separate them. If they're the same person, separate the numbers with commas which you can use to separate the numbers later.

I am attempting this as they are the same person. Logically how would i get the two values linked to the name?

 

 

Use a multidimensional array with the name as a key for the first array and then an array as the value to store multiple numbers.

How would I transfer the data from the array into the multidimensional array? The data i have is in an array where the duplicates are.

 

Iterate over the array and see if any of the elements are the same (so compare every element to the first element, then every element to the second element, etc...). Of course, this would be easier if you could implement a sort/place properly (but that would only apply if you're building the array and not given the array in the first place. 

I kind of understand this. Would this require two loops where the data is compared?

My Rig:

PS4

 

Link to post
Share on other sites

I am attempting this as they are the same person. Logically how would i get the two values linked to the name?

 

How would I transfer the data from the array into the multidimensional array? The data i have is in an array where the duplicates are.

 

I kind of understand this. Would this require two loops where the data is compared?

What language is this?

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to post
Share on other sites

I am attempting this as they are the same person. Logically how would i get the two values linked to the name?

 

How would I transfer the data from the array into the multidimensional array? The data i have is in an array where the duplicates are.

 

I kind of understand this. Would this require two loops where the data is compared?

Yeah, you would need two loops. If you're given the array like that, then at some point you'll have to iterate over two sets of data/loops, so depending on what you need to do with the data afterwards will determine the best way of handling it (i.e either by building a new array like you already have, or converting it to a vector/linked list).

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

How would I transfer the data from the array into the multidimensional array? The data i have is in an array where the duplicates are.

 

Create your new array then while iterating over the current array check to see if the array key exists.

 

If it doesn't exist then create a new entry in your new array with an array as the value with the first number in it.

If it does exist then update the number array with the new number.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×