You are viewing our Forum Archives. To view or take place in current topics click here.
Merging arrays.
Posted:

Merging arrays.Posted:

Gavin-
  • Winter 2020
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
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
  • TTG Addict
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
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-
  • Fairy Master
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
tortuga wrote 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.



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
  • Fairy Master
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Gavin- wrote
tortuga wrote 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.

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
  • TTG Senior
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
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, 201310Year Member
Posts: 2,737
Reputation Power: 448
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
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
  • TTG Senior
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
CriticaI wrote 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++){
    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);
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.