I’ve an array with arrays of different values that I need to re arrange.
Original Array
$cars = [
[2022, "Corolla"],
[2022, "Toyota"],
[2021, "Corolla"],
[2021, "Toyota"],
[2021, "Honda"]
];
Expected Array
$resultCars = [
[2022, "Corolla", "Toyota"],
[2021, "Corolla", "Toyota", "Honda"]
];
4
Answers
Here’s how I would do it in Javascript:
Here is an approach in C++:
As a full example (live demo):
In JS: