admin管理员组

文章数量:1167238

I have an array like var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5]; I really want the output to be [5,2,9,4,5]. My logic for this was:

  • Go through all the element one by one.
  • If the element is the same as the prev element, count the element and do something like newA = arr.slice(i, count)
  • New array should be filled with just identical elements.
  • For my example input, the first 3 elements are identical so newA will be like arr.slice(0, 3) and newB will be arr.slice(3,5) and so on.

I tried to turn this into the following code:

function identical(array){
    var count = 0;
    for(var i = 0; i < array.length -1; i++){
        if(array[i] == array[i + 1]){
            count++;
            // temp = array.slice(i)
        }else{
            count == 0;
        }
    }
    console.log(count);
}
identical(arr);

I am having problems figuring out how to output an element that represents a group of element that are identical in an array. If the element isn't identical it should be outputted in the order that it is in in the original array.

I have an array like var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5]; I really want the output to be [5,2,9,4,5]. My logic for this was:

  • Go through all the element one by one.
  • If the element is the same as the prev element, count the element and do something like newA = arr.slice(i, count)
  • New array should be filled with just identical elements.
  • For my example input, the first 3 elements are identical so newA will be like arr.slice(0, 3) and newB will be arr.slice(3,5) and so on.

I tried to turn this into the following code:

function identical(array){
    var count = 0;
    for(var i = 0; i < array.length -1; i++){
        if(array[i] == array[i + 1]){
            count++;
            // temp = array.slice(i)
        }else{
            count == 0;
        }
    }
    console.log(count);
}
identical(arr);

I am having problems figuring out how to output an element that represents a group of element that are identical in an array. If the element isn't identical it should be outputted in the order that it is in in the original array.

Share Improve this question edited Jun 8, 2015 at 18:58 Patrick M 11k9 gold badges73 silver badges104 bronze badges asked Jun 8, 2015 at 18:49 jack blankjack blank 5,2037 gold badges45 silver badges75 bronze badges 6
  • Can you maybe give a jsfiddle.net? – Michelangelo Commented Jun 8, 2015 at 18:51
  • 1 Why are you slicing the array? – thefourtheye Commented Jun 8, 2015 at 18:51
  • What do you mean with "output an element that represents a group of element"? You can easily remove repetitions but what does this have to do with "output"? Maybe you want a data structure that keeps track of the repetitions albeit the original array is reduced? – pid Commented Jun 8, 2015 at 18:53
  • so what expected output:[5,2,9,4,5] or [[5, 5, 5], [2, 2, 2, 2, 2], 9, 4, [5, 5, 5]] or something else? – Grundy Commented Jun 8, 2015 at 18:55
  • @Grundy [5,2,9,4,5]... i wanted to slice because i didnt know how else to split up the sections that are identical so i could get the number – jack blank Commented Jun 8, 2015 at 18:57
 |  Show 1 more comment

5 Answers 5

Reset to default 40

Using array.filter() you can check if each element is the same as the one before it.

Something like this:

var a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];

var b = a.filter(function(item, pos, arr){
  // Always keep the 0th element as there is nothing before it
  // Then check if each element is different than the one before it
  return pos === 0 || item !== arr[pos-1];
});

document.getElementById('result').innerHTML = b.join(', ');
<p id="result"></p>

if you are looking purely by algorithm without using any function

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];

    function identical(array){

        var newArray = [];
        newArray.push(array[0]);
        for(var i = 0; i < array.length -1; i++) {
            if(array[i] != array[i + 1]) {
                newArray.push(array[i + 1]);
            }
        }
        console.log(newArray);
    }
    identical(arr);

Fiddle;

Yet another way with reduce

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];

var result = arr.reduce(function(acc, cur) {
  if (acc.prev !== cur) {
    acc.result.push(cur);
    acc.prev = cur;
  }
  return acc;
}, {
  result: []
}).result;


document.getElementById('d').innerHTML = JSON.stringify(result);
<div id="d"></div>

A bit hackey, but, hell, I like it.

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];
var arr2 = arr.join().replace(/(.),(?=\1)/g, '').split(',');

Gives you

[5,2,9,4,5]

Admittedly this will fall down if you're using sub-strings of more than one character, but as long as that's not the case, this should work fine.

Try this:

var a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];

uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
});

See Remove Duplicates from JavaScript Array

本文标签: