Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove repeated entries from an array while preserving non-consecutive duplicates?

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.

like image 451
jack blank Avatar asked Jun 08 '15 18:06

jack blank


People also ask

How do I remove a repeating element from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do you prevent duplicate arrays?

To prevent adding duplicates to an array:Use the indexOf() method to check that the value is not present in the array. The indexOf method returns -1 if the value is not contained in the array. If the condition is met, push the value to the array.

What would be the time and space complexity of removing duplicates from N size array?

Altogether, that means the time complexity is O(n), due to the iteration over a . Space complexity is O(n) as well, because the maximum space required is proportional to the size of the input.


2 Answers

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>
like image 134
Rocket Hazmat Avatar answered Sep 30 '22 07:09

Rocket Hazmat


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;

like image 43
Amit.rk3 Avatar answered Sep 30 '22 07:09

Amit.rk3