## ProgrammingMerging arrays.Posted: Tue Aug 08, 2017 12:44 pm

Gavin-
• Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,300
Reputation Power: 821
 Im trying to merge 2 arrays into a 3rd array. Array 1 : Jack , Bob , Bill , Kay Array 2 : Bill , Jill , Sam , Ham Now i want to know what the third array would look like if they were both merged together and their comparisons. Comparison Number 1 : Jack - Bill - Would it be bill that would be inserted as "J" is greater that "B" or does it go by the length of the Strings.? Array 3 : ?
#2. Posted:
tortuga
• Wizard
Status: Offline
Joined: Dec 25, 20098 Year Member
Posts: 2,312
Reputation Power: 530
 Comparing strings by alphabetical order or by length are both valid relations. How you want to compare them is up to you so long as your relation satisfies the properties of a total order. Assuming you're using Java, why not just use the native #compareTo function on strings? You'd still have to decide whether to put the lesser string first or second though.
#3. Posted:
Gavin-
• Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,300
Reputation Power: 821
 tortuga wroteComparing strings by alphabetical order or by length are both valid relations. How you want to compare them is up to you so long as your relation satisfies the properties of a total order. Assuming you're using Java, why not just use the native #compareTo function on strings? You'd still have to decide whether to put the lesser string first or second though. I will probably just do it the alphabetical way because if i get 2 strings of equal length Eg. "Jack" and "Brin" im not sure what i would do so alphabetical seems the way to go.
#4. Posted:
tortuga
• Wizard
Status: Offline
Joined: Dec 25, 20098 Year Member
Posts: 2,312
Reputation Power: 530
 Gavin- wrotetortuga wroteComparing strings by alphabetical order or by length are both valid relations. How you want to compare them is up to you so long as your relation satisfies the properties of a total order. Assuming you're using Java, why not just use the native #compareTo function on strings? You'd still have to decide whether to put the lesser string first or second though. I will probably just do it the alphabetical way because if i get 2 strings of equal length Eg. "Jack" and "Brin" im not sure what i would do so alphabetical seems the way to go. You could always just compare them by length first, and then fall back to comparing them alphabetically whenever the lengths are equal.
#5. Posted:
Cyimking
• V5 Launch
Status: Offline
Joined: May 02, 20126 Year Member
Posts: 1,117
Reputation Power: 33
 1. What language are you using? 2. What is an accepted OUTPUT? Are duplicates allowed? If so, then you will have duplicates, else you will only have one copy. I believe you are looking for Lexical analysis. Then you can compare strings.
#6. Posted:
CriticaI
• 2 Million
Status: Offline
Joined: Nov 05, 20134 Year Member
Posts: 2,638
Reputation Power: 153
 Idk about other languages, but in javascript you can do the following if you're trying to merge and alphabetize arrays (probably not dry) how to merge arrays ```var array1 = ['ball', 'cat', 'dog'] var array2 = ['air', 'ball', 'elephant'] var array3 = [] for (i=0; i

Last edited by CriticaI ; edited 1 time in total
#7. Posted:
Cyimking
• V5 Launch
Status: Offline
Joined: May 02, 20126 Year Member
Posts: 1,117
Reputation Power: 33
 CriticaI wroteIdk about other languages, but in javascript you can do the following if you're trying to merge and alphabetize arrays (probably not dry) how to merge arrays ```var array1 = ['ball', 'cat', 'dog'] var array2 = ['air', 'ball', 'elephant'] var array3 = [] for (i<0; i array1 = new HashSet(Arrays.asList(array1)); Set array2 = new HashSet(Arrays.asList(array2)); Set array3 = array2.addAll(array1); // TreeSet will automatically sort this for you TreeSet sortedArray = new TreeSet(); sortedArray.addAll(array3); ```
Users browsing this topic: None