ProgrammingMerging arrays.Posted:

Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
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, 20097 Year Member
Posts: 2,307
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
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,199
Reputation Power: 765
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
  • E3 2017
Status: Offline
Joined: Dec 25, 20097 Year Member
Posts: 2,307
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
Gavin- wrote
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.

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
  • 2 Million
Status: Offline
Joined: May 02, 20125 Year Member
Posts: 1,100
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
  • Summer Giveaway
Status: Offline
Joined: Nov 05, 20134 Year Member
Posts: 2,592
Reputation Power: 146
Motto: Do What You Can't
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<array1.length; i++){
    array3.push(array1[i])
}
for (j=0; j<array2.length; j++){
    array3.push(array2[j])
}


how to merge but check for duplicates

var array1 = ['ball', 'cat', 'dog']
var array2 = ['air', 'ball', 'elephant']
var array3 = []
for (i=0; i<array1.length; i++){
    array3.push(array1[i])
}
for (j=0; j<array2.length; j++){
    var found = false
    for (i=0; i<array3.length; i++){
        if (array2[j] == array3[i]) found = true
    }
    if (!found) array3.push(array2[j])
}



finally sort the array by doing
array3.sort()


edited some stuff. had many typos


Last edited by CriticaI ; edited 1 time in total
#7. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 20125 Year Member
Posts: 1,100
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.length; i++){
    array3.push(array1[i])
}
for (j<0; j<array2.length; j++){
    array3.push(array2[j])
}


how to merge but check for duplicates
var array1 = ['ball', 'cat', 'dog']
var array2 = ['air', 'ball', 'elephant']
var array3 = []
for (i<0; i<array1.length; i++){
    array3.push(array1[i])
}
for (j<0; j<array2.length; j++){
    for (i<0; i<array1.length; i++){
        if (array1[i] != array2[j]) array3.push(array2[j])
    }
}


finally sort the array by doing
array3.sort()


Is this what he was asking for? If so then...
PHP

$array3 = array_unique(array_merge($array1, $array2), SORT_REGULAR);
sort($array3);


Java

// When merging hashsets, duplicates are not added since hashsets do not allow duplicates.
Set<T> array1 = new HashSet<T>(Arrays.asList(array1));
Set<T> array2 = new HashSet<T>(Arrays.asList(array2));
Set<T> array3 = array2.addAll(array1);

// TreeSet will automatically sort this for you
TreeSet sortedArray = new TreeSet();
sortedArray.addAll(array3);
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS