FerO
FerO

Reputation: 1

After creating a random javaScript-array with recurring integers - avoid identical integers side by side

have anybody any suggestion to "deal" a array after filling it randomly with recurring integers?

here is a easy example (3 x integer-group: 1,2,3,4):

array[1,2,3,4,4,2,3,1,1,4,3,2];

is there a way with a special function, to "rearrange" the array, by avoiding the same integers side by side.

this would be OK (no immediate neighbor is identical):

array[1,2,1,2,1,3,4,3,4,3,4,2];

those are not:

array[4,4,4,1,2,3,3,2,1,1,2,3];
array[1,2,3,2,1,3,3,4,4,1,2,4];

in my case the array could have 25 to 30 times the same integer-group. hopefully I'm declaring comprehensible - so, you understanding my problem!

Thank you in advance for your efforts

FerO

Edited for clarity:

All integers must be conserved (not deleted) and the integers could be between 0 and 99!

changed "clean" to "rearrange"

Upvotes: 0

Views: 107

Answers (2)

S McCrohan
S McCrohan

Reputation: 6693

Okay, now that I understand what you're after better - here you are. First, a caveat: there are arrays for which this is impossible. For instance, any array in which more than half the elements have the same value cannot be shuffled to keep those values away from each other.

That said:

var insert = function(val,arr) {
    if (arr.length > 1) {
        for (var i=1;i<arr.length;i++) {
            if (arr[i-1] !=val && arr[i] != val) {
                arr.splice(i,0,val);
                return arr;
            }
        }
    }
    arr.splice(0,0,val);
    return arr;
}

var shuffle = function(arr) {
    return arr.reduce(function(p,c,i,a) {
        return insert(c,p);
    }, []);    
}

shuffle() returns a new array built by shuffling the values of the input array; if you'd rather have them shuffled 'in place' by mutating the input array, that's easy enough to do. There may exist theoretically-shuffleable arrays which this algorithm fails for. I haven't found any in brief testing, but I also haven't proven they don't exist.

The algorithm here is:

  1. Start with an empty 'destination' array.
  2. For each element in the input array, traverse the destination array and insert it into the first position found where it does not equal either of its neighbors.
  3. If no such position is found, stick it at the front.

(This would perform slightly better if it weren't ignoring the possibility of putting things in position 0 right off)

shuffle([4,4,4,3,3,3,2,2,2,1,1,1,1]) = [3, 1, 2, 1, 4, 1, 2, 1, 3, 2, 4, 3, 4] shuffle([1,2,3,4,4,2,3,1,1,4,3,2,2]) = [2, 3, 2, 4, 2, 1, 3, 1, 4, 2, 3, 4, 1] shuffle([1,1,1,1,1,1,1,2,2,2,2,2,2,2]) = [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]

JSfiddle

Upvotes: 1

Trott
Trott

Reputation: 70065

You can use Array.prototype.filter() to remove values that are adjacent to identical values.

var myArray = [1, 2, 3, 4, 4, 2, 3, 1, 1, 4, 3, 2];

var deduped = myArray.filter(function (value, index, collection) { 
    return value !== collection[index+1];
});

// deduped is now [1, 2, 3, 4, 2, 3, 1, 4, 3, 2]

Upvotes: 0

Related Questions