Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I must say your question is rather unclear, but i think i got it.</p> <p>In other words what you're saying is: I have an array containing a bunch of numbers, logically they represent coordinates, it's not that the coordinates are subarrays inside the master array, is just looking them 2 by 2, but it's a linear array.</p> <p>What you want is something that detects coordinates that are adjacent and generate a new array containing them.</p> <p>After that you want to go thru the new arrays and generate new arrays containing unique-elements.</p> <p>Well that's the question, now the answer. First, the second point depends on how far you want to go, i'm thinking it's anormal grid of x,y coordinates, but how adjacent you want to go? The following just applies to the inmediate adjacent, up to 8 points can be adjacent to a single point.</p> <pre><code>[1,1][2,1][3,1] [1,2][2,2][3,2] [1,3][2,3][3,3] </code></pre> <p>May that be a representation of the grid, if your master array has the [2,2] coordinate, you want to build an array that begins with that one and all adjacents you find, lets say like master array has [3,2], then you want to add it to the subarray of [2,2].</p> <p>I'm really not writing the code i'm just gonna explain sorts of algorithm you could use. To build the second point arrays, lets call them Adjacents Arrays (AA) you could:</p> <p>First coordinate will always build the first AA To find adjacents you will cycle thru the master array and perform an "Adjacency Check" to every coordinate which would be: second x == ( first x-1, x or x+1) AND second y == ( first y-1, y or y+1), if it passes then pop/push, if not... next. In case you finish cycling thru the master array means that AA is complete, and you have to start a new AA with the next coordinate. Repeat until master array is empty.</p> <p>Then to create the unique-element-array is quite a simple cycle, i wrote a similar function that does something like that but it creates an array with the element and how many times it appears in the array (instances):</p> <pre><code>function uniqueCnt( ori) { // agroups and counts unique elements of an array, scrubs '' elements var res = []; // resulting array, ori parameter stands for original array for( let cntA = 0; cntA &lt; ori.length; cntA++) { for( cntB = 0; cntB &lt; res.length; cntB += 2) if( ori[cntA] == res[cntB]) { res[cntB + 1]++; break; } // if it matches means it's another instance then increase that element count if( cntB == res.length &amp;&amp; ori[cntA] != '') res.push( ori[cntA], 1); // New element found then push it and start count } return res; // returns the agrouped array 0:element 1:instances... } </code></pre> <p>If you don't want a count of instances, then you would need an even simpler function, you could try modify this one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload