Convert 2 Dimensional Array

Convert a 2D array into a 1D array

You've almost got it right. Just a tiny change:

public static int mode(int[][] arr) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
// tiny change 1: proper dimensions
for (int j = 0; j < arr[i].length; j++) {
// tiny change 2: actually store the values
list.add(arr[i][j]);
}
}

// now you need to find a mode in the list.

// tiny change 3, if you definitely need an array
int[] vector = new int[list.size()];
for (int i = 0; i < vector.length; i++) {
vector[i] = list.get(i);
}
}

How to convert 2 dimensional array to Set?

A simple (but not the best performing) solution would be to use java streams:

Set<Foo> set = Arrays.stream(array)
.flatMap(Arrays::stream)
.collect(Collectors.toSet());

The above code snippet first creates a Stream<Foo[]> with the Arrays.stream(array) statement.

Then it flattens that stream into Stream<Foo> with the second statement: .flatMap(Arrays::stream) which behaves the same as .flatMap(arr -> Arrays.stream(arr)).

At last it creates a Set<Foo> out of the flattened stream with .collect(Collectors.toSet()).

I suggest having a deep look at the Java Streaming API, introduced with Java 8. It can do much more than just mapping a 2d array into a Set.


Another approach is just to use 2 nested for loops:

Set<Foo> set = new HashSet<>(); // or LinkedHashSet if you need a similar order than the array
for(Foo[] inner : array) {
for(Foo item : inner) {
set.add(item);
}
}

How to convert multidimensional array into a 2 dimensional array?

also, next time you can try to read documentation :)

a = [[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5]], [[1, 0], [1, 1], [1, 2],   [1, 3], [1, 4], [1, 5]], [[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], [[3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5]], [[4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5]], [[5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]]

a.flatten(1)
>[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]

Convert two dimensional array to one dimensional array in Java

We create a single Integer array with a length determines by the size method. Size takes the 2-dimensional array and finds the sum of all columns in each row and returns that sum. We check if the length is 0, and if so, we return. We then loop through all rows, followed by a loop through all columns in that row. We then assign the value at that row and column to that next index in the array. Unfortunately we can't do any math (to my knowledge) to determine the index based of row/column since they could be variable sizes.

    public static int[] twoDConvert(int[][] nums) {
int[] combined = new int[size(nums)];

if (combined.length <= 0) {
return combined;
}
int index = 0;

for (int row = 0; row < nums.length; row++) {
for (int column = 0; column < nums[row].length; column++) {
combined[index++] = nums[row][column];
}
}
return combined;
}

private static int size(int[][] values) {
int size = 0;

for (int index = 0; index < values.length; index++) {
size += values[index].length;
}
return size;
}

Convert a 2D JavaScript array to a 1D array

Try .concat():

var arrToConvert = [[0,0,1],[2,3,3],[4,4,5]];
var newArr = [];

for(var i = 0; i < arrToConvert.length; i++)
{
newArr = newArr.concat(arrToConvert[i]);
}

console.log(newArr);

How to convert a two dimension array into a one dimension array in GO?

You can use the "bytes".Join function from the standard library:

result := bytes.Join(chunks, nil)

First argument is your slice of slices ([][]byte), second argument is the separator (aka glue). In your case, the separator is an empty slice of bytes (nil works as well).

In the playground: https://play.golang.org/p/8pquRk7PDo.

How to convert 3 dimensional array into 2 dimensional array in PHP?

Your code is close; you can generate the output you want with nested foreach loops, but you have to save the keys at each level into the category and attribute values, and then push a value into the result array in the innermost loop:

$result = [];
foreach ($data as $category => $attributes) {
foreach ($attributes as $attribute => $options) {
foreach ($options as $option) {
$result[] = array('category' => $category, 'attribute' => $attribute, 'option' => $option);
}
}
}

Output:

Array
(
[0] => Array
(
[category] => 1
[attribute] => 1
[option] => 1
)
[1] => Array
(
[category] => 1
[attribute] => 1
[option] => 2
)
[2] => Array
(
[category] => 1
[attribute] => 2
[option] => 3
)
[3] => Array
(
[category] => 1
[attribute] => 2
[option] => 4
)
[4] => Array
(
[category] => 2
[attribute] => 3
[option] => 5
)
[5] => Array
(
[category] => 2
[attribute] => 3
[option] => 6
)
[6] => Array
(
[category] => 2
[attribute] => 4
[option] => 7
)
[7] => Array
(
[category] => 2
[attribute] => 4
[option] => 8
)
)

Demo on 3v4l.org

How to convert a multi-dimensional array to a dictionary?

Sometimes not using linq is easier to read and faster:

 var dict = new Dictionary<string, string[]>();
for (int i = 0; i < arr.GetLength(0); i++)
dict[arr[i, 0]] = new string[] { arr[i, 1], arr[i, 2] };

But when you feel like you REALLY need to use linq:

 Enumerable.Range(0, arr.GetLength(0))
.ToDictionary(i => arr[i, 0], i => new string[] {arr[i, 1], arr[i, 2]});

How to convert 2-D array to 1-D array fast in C#?

The fastest way is one of the direct memory copy methods, like Buffer.BlockCopy, or Marshal.Copy.

Example

var ary1 = new int[3,3];
ary1[0, 0] = 0;
ary1[0, 1] = 1;
ary1[0, 2] = 2;
ary1[1, 0] = 3;
ary1[1, 1] = 4;
ary1[1, 2] = 5;
ary1[2, 0] = 6;
ary1[2, 1] = 7;
ary1[2, 2] = 8;

var ary2 = new int[9];

Buffer.BlockCopy(ary1, 0, ary2, 0, 9 * sizeof(int));

Console.WriteLine(string.Join(",", ary2));

Output

0,1,2,3,4,5,6,7,8

You could also use memcpy, however you will incur the initial overhead from the PInvoke

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);

Benchmarks

The benchmarks were run 100 times, garbage collected after each run, and validated against each other for accuracy, the results are collated from the top 75% of the fasted runs, the scale is the dimension length of a multidimensional array ie double[scale,scale]

┌──────────────────┬────────────────────────────────────────────┐
│ Test Mode │ Release (64Bit) │
│ Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro │
│ Version │ 10.0.17763 │
├──────────────────┼────────────────────────────────────────────┤
│ CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz │
│ CPU Description │ Intel64 Family 6 Model 158 Stepping 9 │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│ Cores (Threads) │ 4 (8) │ Architecture │ x64 │
│ Clock Speed │ 3600 MHz │ Bus Speed │ 100 MHz │
│ L2Cache │ 1 MB │ L3Cache │ 8 MB │
└──────────────────┴──────────┴──────────────────┴──────────────┘

Results for byte array

┌── Standard input ──────────────────────────────────────────────────────────────────────┐
│ Value │ Average │ Fastest │ Cycles │ Garbage │ Test │ Gain │
├── Scale 256 ────────────────────────────────────────────────────────────── 0.628 sec ──┤
│ MarshalCopy │ 6.450 µs │ 4.300 µs │ 26.698 K │ 0.000 B │ Pass │ 24.21 % │
│ memcpy │ 7.992 µs │ 4.700 µs │ 32.758 K │ 0.000 B │ Pass │ 6.09 % │
│ BlockCopy │ 8.511 µs │ 4.600 µs │ 37.053 K │ 0.000 B │ Base │ 0.00 % │
│ ElemCopy Unsafe │ 26.124 µs │ 24.400 µs │ 97.794 K │ 0.000 B │ Pass │ -206.96 % │
│ ElemCopy │ 75.426 µs │ 72.300 µs │ 273.201 K │ 0.000 B │ Pass │ -786.27 % │
│ Linq │ 7.619 ms │ 7.078 ms │ 27.103 M │ 0.000 B │ Pass │ -89,429.16 % │
├── Scale 512 ────────────────────────────────────────────────────────────── 1.826 sec ──┤
│ MarshalCopy │ 17.939 µs │ 17.300 µs │ 68.142 K │ 0.000 B │ Pass │ 1.33 % │
│ BlockCopy │ 18.182 µs │ 17.300 µs │ 69.770 K │ 0.000 B │ Base │ 0.00 % │
│ memcpy │ 25.897 µs │ 19.200 µs │ 97.357 K │ 0.000 B │ Pass │ -42.44 % │
│ ElemCopy Unsafe │ 128.776 µs │ 102.400 µs │ 471.381 K │ 0.000 B │ Pass │ -608.28 % │
│ ElemCopy │ 293.237 µs │ 285.400 µs │ 1.055 M │ 0.000 B │ Pass │ -1,512.82 % │
│ Linq │ 31.057 ms │ 29.750 ms │ 110.869 M │ 0.000 B │ Pass │ -170,714.99 % │
├── Scale 1,024 ──────────────────────────────────────────────────────────── 6.579 sec ──┤
│ memcpy │ 268.747 µs │ 255.600 µs │ 972.409 K │ 0.000 B │ Pass │ 12.28 % │
│ BlockCopy │ 306.371 µs │ 291.500 µs │ 1.104 M │ 0.000 B │ Base │ 0.00 % │
│ MarshalCopy │ 307.868 µs │ 293.100 µs │ 1.111 M │ 0.000 B │ Pass │ -0.49 % │
│ ElemCopy Unsafe │ 583.684 µs │ 561.100 µs │ 2.103 M │ 0.000 B │ Pass │ -90.52 % │
│ ElemCopy │ 1.325 ms │ 1.305 ms │ 4.768 M │ 0.000 B │ Pass │ -332.50 % │
│ Linq │ 122.561 ms │ 120.700 ms │ 439.940 M │ 0.000 B │ Pass │ -39,904.01 % │
├── Scale 2,048 ─────────────────────────────────────────────────────────── 26.084 sec ──┤
│ memcpy │ 1.179 ms │ 1.129 ms │ 4.230 M │ 0.000 B │ Pass │ 17.50 % │
│ MarshalCopy │ 1.397 ms │ 1.346 ms │ 5.029 M │ 0.000 B │ Pass │ 2.25 % │
│ BlockCopy │ 1.429 ms │ 1.360 ms │ 5.135 M │ 0.000 B │ Base │ 0.00 % │
│ ElemCopy Unsafe │ 2.441 ms │ 2.312 ms │ 8.757 M │ 0.000 B │ Pass │ -70.88 % │
│ ElemCopy │ 5.466 ms │ 5.264 ms │ 19.587 M │ 0.000 B │ Pass │ -282.61 % │
│ Linq │ 497.788 ms │ 489.885 ms │ 1.786 B │ 0.000 B │ Pass │ -34,743.98 % │
├── Scale 4,096 ─────────────────────────────────────────────────────────── 42.833 sec ──┤
│ memcpy │ 5.218 ms │ 4.889 ms │ 18.633 M │ 0.000 B │ Pass │ 15.45 % │
│ BlockCopy │ 6.172 ms │ 5.887 ms │ 22.141 M │ 0.000 B │ Base │ 0.00 % │
│ MarshalCopy │ 6.255 ms │ 5.871 ms │ 22.350 M │ 0.000 B │ Pass │ -1.35 % │
│ ElemCopy Unsafe │ 9.972 ms │ 9.535 ms │ 35.716 M │ 0.000 B │ Pass │ -61.57 % │
│ ElemCopy │ 22.149 ms │ 21.741 ms │ 79.508 M │ 0.000 B │ Pass │ -258.87 % │
│ Linq │ 1.969 s │ 1.948 s │ 7.067 B │ 0.000 B │ Pass │ -31,796.88 % │
└────────────────────────────────────────────────────────────────────────────────────────┘

Results for double array

┌── Standard input ─────────────────────────────────────────────────────────────────────┐
│ Value │ Average │ Fastest │ Cycles │ Garbage │ Test │ Gain │
├── Scale 256 ───────────────────────────────────────────────────────────── 0.688 sec ──┤
│ BlockCopy │ 35.116 µs │ 32.000 µs │ 131.112 K │ 0.000 B │ Base │ 0.00 % │
│ MarshalCopy │ 43.879 µs │ 34.900 µs │ 162.031 K │ 0.000 B │ Pass │ -24.96 % │
│ ElemCopy Unsafe │ 44.182 µs │ 39.500 µs │ 162.891 K │ 0.000 B │ Pass │ -25.82 % │
│ memcpy │ 60.113 µs │ 58.200 µs │ 219.950 K │ 0.000 B │ Pass │ -71.19 % │
│ ElemCopy │ 94.418 µs │ 86.100 µs │ 356.173 K │ 0.000 B │ Pass │ -168.88 % │
│ Linq │ 7.402 ms │ 7.123 ms │ 26.638 M │ 0.000 B │ Pass │ -20,979.54 % │
├── Scale 512 ───────────────────────────────────────────────────────────── 2.237 sec ──┤
│ memcpy │ 612.603 µs │ 552.000 µs │ 2.199 M │ 0.000 B │ Pass │ 10.20 % │
│ ElemCopy Unsafe │ 641.013 µs │ 586.200 µs │ 2.312 M │ 0.000 B │ Pass │ 6.03 % │
│ MarshalCopy │ 675.192 µs │ 621.200 µs │ 2.434 M │ 0.000 B │ Pass │ 1.02 % │
│ BlockCopy │ 682.161 µs │ 622.700 µs │ 2.458 M │ 0.000 B │ Base │ 0.00 % │
│ ElemCopy │ 745.692 µs │ 708.800 µs │ 2.687 M │ 0.000 B │ Pass │ -9.31 % │
│ Linq │ 33.579 ms │ 31.039 ms │ 119.974 M │ 0.000 B │ Pass │ -4,822.46 % │
├── Scale 1,024 ─────────────────────────────────────────────────────────── 7.830 sec ──┤
│ memcpy │ 2.708 ms │ 2.488 ms │ 9.712 M │ 0.000 B │ Pass │ 20.63 % │
│ ElemCopy Unsafe │ 3.156 ms │ 2.789 ms │ 11.324 M │ 0.000 B │ Pass │ 7.51 % │
│ MarshalCopy │ 3.208 ms │ 2.979 ms │ 11.508 M │ 0.000 B │ Pass │ 5.97 % │
│ ElemCopy │ 3.342 ms │ 3.091 ms │ 12.021 M │ 0.000 B │ Pass │ 2.05 % │
│ BlockCopy │ 3.412 ms │ 2.959 ms │ 12.234 M │ 0.000 B │ Base │ 0.00 % │
│ Linq │ 125.854 ms │ 122.872 ms │ 451.735 M │ 0.000 B │ Pass │ -3,588.76 % │
├── Scale 2,048 ────────────────────────────────────────────────────────── 29.876 sec ──┤
│ memcpy │ 10.989 ms │ 10.288 ms │ 39.509 M │ 0.000 B │ Pass │ 15.14 % │
│ ElemCopy Unsafe │ 12.075 ms │ 11.418 ms │ 43.436 M │ 0.000 B │ Pass │ 6.76 % │
│ BlockCopy │ 12.950 ms │ 12.462 ms │ 46.578 M │ 0.000 B │ Base │ 0.00 % │
│ MarshalCopy │ 13.032 ms │ 12.427 ms │ 46.876 M │ 0.000 B │ Pass │ -0.64 % │
│ ElemCopy │ 13.469 ms │ 12.689 ms │ 48.471 M │ 0.000 B │ Pass │ -4.01 % │
│ Linq │ 502.897 ms │ 497.335 ms │ 1.805 B │ 0.000 B │ Pass │ -3,783.35 % │
├── Scale 4,096 ────────────────────────────────────────────────────────── 58.669 sec ──┤
│ memcpy │ 45.901 ms │ 44.148 ms │ 164.750 M │ 0.000 B │ Pass │ 15.80 % │
│ ElemCopy Unsafe │ 51.889 ms │ 50.497 ms │ 186.137 M │ 0.000 B │ Pass │ 4.82 % │
│ MarshalCopy │ 53.237 ms │ 51.847 ms │ 191.248 M │ 0.000 B │ Pass │ 2.34 % │
│ BlockCopy │ 54.514 ms │ 52.417 ms │ 195.778 M │ 0.000 B │ Base │ 0.00 % │
│ ElemCopy │ 56.551 ms │ 54.674 ms │ 203.163 M │ 0.000 B │ Pass │ -3.74 % │
│ Linq │ 2.004 s │ 1.976 s │ 7.192 B │ 0.000 B │ Pass │ -3,575.84 % │
└───────────────────────────────────────────────────────────────────────────────────────┘

Test Code

[Test("BlockCopy", "", true)]
public double[] Test1(double[,] input, int scale)
{
var width = input.GetLength(0);
var height = input.GetLength(1);
var size = width * height;
var result = new double[size];
Buffer.BlockCopy(input, 0, result, 0, size * sizeof(double));
return result;
}

[Test("MarshalCopy", "", false)]
public unsafe double[] Test2(double[,] input, int scale)
{
var width = input.GetLength(0);
var height = input.GetLength(1);
var size = width * height;
var result = new double[size];
fixed (double* pInput = input)
Marshal.Copy((IntPtr)pInput, result, 0, size );
return result;
}

[Test("ElemCopy", "", false)]
public double[] Test3(double[,] input, int scale)
{
var width = input.GetLength(0);
var height = input.GetLength(1);
var size = width * height;

var result = new double[size];
for (var i = 0; i < width; i++)
for (var j = 0; j < height; j++)
result[i * height + j] = input[i,j];
return result;
}
[Test("ElemCopy Unsafe", "", false)]
unsafe public double[] Test4(double[,] input, int scale)
{
var width = input.GetLength(0);
var height = input.GetLength(1);
var size = width * height;

var result = new double[size];
fixed (double* pInput = input, pResult = result)
for (var i = 0; i < width; i++)
for (var j = 0; j < height; j++)
*(pResult + i * height + j) = *(pInput + i * height + j);
return result;
}
[Test("memcpy", "", false)]
unsafe public double[] Test5(double[,] input, int scale)
{
var width = input.GetLength(0);
var height = input.GetLength(1);
var size = width * height;

var result = new double[size];
fixed (double* pInput = input, pResult = result)
memcpy((IntPtr)pResult, (IntPtr)pInput, (UIntPtr)(size * sizeof(double)));
return result;
}
[Test("Linq", "", false)]
unsafe public double[] Test6(double[,] input, int scale)
{
return input.OfType<double>().ToArray();
}

Note : You should probably run these tests yourself on your own spec pcs, framework and such and should only be used as a guide

Convert 2D array to object using map or reduce in javascript

If supported, you can use Object.fromEntries() to convert an array of [key, value] pairs to an object. In this case, the pairs are [value, key], so we'll need to map them first to an array of [key, value] pairs.

const arr = [[35, "Bill"], [20, "Nancy"], [27, "Joan"]];

const obj = Object.fromEntries(arr.map(([v, k]) => [k, v]));

console.log(obj);


Related Topics



Leave a reply



Submit