Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

388
Views
Zigzag traversal of a two-dimensional array

I need to traverse a two-dimensional array in a zigzag and pick the elements along the way:

From:

[['๐ŸŒ','๐ŸŽ','๐Ÿ˜ƒ','๐Ÿ‰'],
 ['๐Ÿ‘บ','๐Ÿบ','๐Ÿฉ','๐Ÿšด'],
 ['๐Ÿš˜','๐Ÿฆ‘','๐Ÿš†','๐Ÿ'],
 ['๐ŸŒ†','๐Ÿ›น','๐Ÿ•บ','๐Ÿ•']]

To:

['๐ŸŒ','๐Ÿ‘บ','๐ŸŽ','๐Ÿ˜ƒ','๐Ÿบ','๐Ÿš˜','๐ŸŒ†','๐Ÿฆ‘','๐Ÿฉ','๐Ÿ‰','๐Ÿšด','๐Ÿš†','๐Ÿ›น','๐Ÿ•บ','๐Ÿ','๐Ÿ•']

My approach was to use a for loop, check each index of the first array and compare it against the index of the next array and then if that number is bigger by one push it into the new one dimensional array.

What is the best approach to solve this? Do you have some resources to learn more about this pattern?

about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

My understanding is that you want to transform a nร—n array such as:

[ ['๐Ÿ˜ƒ', '๐ŸŒฏ', '๐Ÿป', '๐Ÿ™ƒ']

, ['๐Ÿ˜ˆ', '๐ŸŒฝ', '๐Ÿ’ฅ', '๐Ÿ”']

, ['๐Ÿ–', '๐Ÿฅ‘', '๐Ÿฃ', '๐Ÿฅฆ']

, ['๐ŸŒฎ', '๐Ÿงบ', '๐Ÿ˜Ž', '๐Ÿฆ‘'] ]

into:

['๐Ÿ˜ƒ','๐Ÿ˜ˆ','๐ŸŒฏ','๐Ÿป','๐ŸŒฝ','๐Ÿ–','๐ŸŒฎ','๐Ÿฅ‘','๐Ÿ’ฅ','๐Ÿ™ƒ','๐Ÿ”','๐Ÿฃ','๐Ÿงบ','๐Ÿ˜Ž','๐Ÿฅฆ','๐Ÿฆ‘']

Let's transform the original array into a "matrix of positions" and let's try to picture the "zigzag":

[ [[0,0], [0,1], [0,2], [0,3]]
// โ†™      โ†—      โ†™      โ†—
, [[1,0], [1,1], [1,2], [1,3]]
// โ†—      โ†™      โ†—      โ†™
, [[2,0], [2,1], [2,2], [2,3]]
// โ†™      โ†—      โ†™      โ†—
, [[3,0], [3,1], [3,2], [3,3]]
// โ†—      โ†™      โ†—      โ†™
]

If we focus on the edges we can start working out a pattern:

[ [0,0]
, [1,0], /* โ€ฆ */ [0,1]
, [2,0], /* โ€ฆ */ [0,2]
, [3,0], /* โ€ฆ */ [0,3]
, [3,1], /* โ€ฆ */ [1,3]
, [3,2], /* โ€ฆ */ [2,3]
,                [3,3] ]

Now we need to work out all the [x,y] between each edges and traverse each edge in opposite direction:

const inp1 = zigzag([ ['๐Ÿ˜ƒ', '๐ŸŒฏ', '๐Ÿป', '๐Ÿ™ƒ']

                    , ['๐Ÿ˜ˆ', '๐ŸŒฝ', '๐Ÿ’ฅ', '๐Ÿ”']

                    , ['๐Ÿ–', 'โ˜๏ธ', '๐Ÿฃ', '๐Ÿฅฆ']

                    , ['๐ŸŒฎ', '๐Ÿงบ', '๐Ÿ˜Ž', '๐Ÿฆ‘'] ]);

const inp2 = zigzag([ ['๐Ÿ˜ƒ', '๐ŸŒฏ', '๐Ÿป']

                    , ['๐Ÿ˜ˆ', '๐ŸŒฝ', '๐Ÿ’ฅ']

                    , ['๐Ÿ–', 'โ˜๏ธ', '๐Ÿฃ'] ]);

const inp3 = zigzag([ ['๐Ÿ˜ƒ', '๐ŸŒฏ']

                    , ['๐Ÿ˜ˆ', '๐ŸŒฝ'] ]);

const inp4 = zigzag([ ['๐Ÿ˜ƒ'] ]);

console.log(`
  [${String(inp1)}]
  [${String(inp2)}]
  [${String(inp3)}]
  [${String(inp4)}]
`);
<script>
const zigzag = inp => {
  const m = inp.length - 1;
  const edges = [];
  for (let x = 0; x <= m; x++) edges.push([x, 0]);
  for (let x = 1; x <= m; x++) edges.push([m, x]);
  return edges.flatMap(([x, y], i) => {
    const path = [[x, y]];
    for (let a = x, b = y; a != y && b != x;) path.push([--a, ++b]);
    return (i % 2 ? path : path.reverse()).map(([x, y]) => inp[x][y]);
  });
}
</script>

about 4 years ago ยท Juan Pablo Isaza Report

0

OLD ANSWER:

you can use .flat() method for javascript array. Array.flat()

let array = [
    [1, 3, 4, 10],
    [2, 5, 9, 11],
    [6, 8, 12, 15],
    [7, 13, 14, 16],
]
const flatArray = array.flat()
flatArray.sort((a,b)=>a-b)
console.log(flatArray)

UPDATE ANSWER: after question update output

const items = [
    [1, 3, 4, 10],
    [2, 5, 9, 11],
    [6, 8, 12, 15],
    [7, 13, 14, 16],
];

/*const items =  [
  [๐ŸŒ , ๐ŸŽ , ๐Ÿ˜ƒ , ๐Ÿ‰ ],
  [๐Ÿ‘บ , ๐Ÿบ , ๐Ÿฉ , ๐Ÿšด ],
  [๐Ÿš˜ , ๐Ÿช„ , ๐Ÿš† , ๐Ÿ ],
  [๐ŸŒ† , ๐Ÿ›น , ๐Ÿ•บ , ๐Ÿ• ],
]*/

function zigZag(arr) {
    let array = []
    const itemCounts = arr.reduce((pre, cur)=> pre+cur.length,0)    
    for(let i=0; i<itemCounts; i+=1){
        let round = []
        for(let j=0; j<arr.length; j+=1){
            if(arr[j].length){
                round.push({
                    value: arr[j][0],
                    row:j
                })
            }
            
        }
        const minValue = Math.min(...round.map(item=>item.value))
        const target = round.find(item=>item.value == minValue)
        array.push(arr[target.row].shift())
    }    
    return array;
};

console.log(zigZag(items))

about 4 years ago ยท Juan Pablo Isaza Report

0

UPDATED ANSWER

This function will merge arrays in zigZag way.

Here I have shown example with 2 arrays with different data type values.

function zigZag(array) {
    let arrayLength = array.length;
    let arrayItemLength = array[0].length;
    let result = [];
    let flag = true;

    for(let i = 0; i < (arrayLength + (arrayLength / 2) + 1) ; i++) {
        if(i < arrayItemLength) {
            let length = (i + 1);
            let ii = i;
            for(let j = 0; j < length; j++) {
                if(flag == true) result.push(array[j][ii]);
                else result.push(array[ii][j]);
                ii-=1;
            }
        }else {
            let ii = (i + 1) - arrayItemLength;

            for(let j = arrayItemLength - 1; j > i - arrayItemLength; j--) {
                if(flag == true) result.push(array[ii][j]);
                else result.push(array[j][ii]);
                ii+=1;
            }
        }
        if(flag == true) flag = false;
        else flag = true;
    }

    return result;
}

let array = [
    ["๐ŸŒ" , "๐ŸŽ" , "๐Ÿ˜ƒ" , "๐Ÿ‰" ],
    ["๐Ÿ‘บ" , "๐Ÿบ" , "๐Ÿฉ" , "๐Ÿšด" ],
    ["๐Ÿš˜" , "๐Ÿช„" , "๐Ÿš†" , "๐Ÿ" ],
    ["๐ŸŒ†" , "๐Ÿ›น" , "๐Ÿ•บ" , "๐Ÿ•" ],
];

let array_1 = [
    [1, 3, 4, 10],
    [2, 5, 9, 11],
    [6, 8, 12, 15],
    [7, 13, 14, 16],
];

console.log(zigZag(array)); // icons
console.log(zigZag(array_1)); // numbers

OLD ANSWER

Try this, I think this what you want to do.

let array = [
    [1, 3, 4, 10],
    [2, 5, 9, 11],
    [6, 8, 12, 15],
    [7, 13, 14, 16],
];

function mergeArray(array) {
    let merged = array.reduce((item, total) => [...total, ...item], []);
    return merged.sort((a, b) => a - b);
}

let result = mergeArray(array);

console.log(result)

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!