Score: 10

# A modern one-liner using .filter()

## `arr.filter((e, i, a) => e !== a[i - 1]);`

I'm very surprised by the complexity of 6 other answers here, even those that use 5 `.filter()`

Even using old-school ES5 syntax with no 4 arrow functions:

``````arr.filter(function (e, i, a) { return e !== a[i - 1] });
``````

Example:

``````let a = [0, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9, 9];

let b = arr.filter((e, i, a) => e !== a[i - 1]);

console.log(b); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
``````

If you need to mutate 3 the array in place then just use:

``````arr = arr.filter((e, i, a) => e !== a[i - 1]);
``````

Personally 2 I would recommend against using such complex 1 solutions as the ones in other answers here.

Score: 3

This is a one-liner:

``````uniquify( myArray.filter(function(x){return true}) )
``````

If you don't already 21 have `uniquify` written (the function you wrote to 20 remove duplicates), you could also use this 19 two-liner:

``````var newArray = [];
myArray.forEach(function(x) {
if (newArray.length==0 || newArray.slice(-1)!==x)
newArray.push(x)
})
``````

Elaboration:

``````var a=[];
a=1; a=undefined; a=undefined;
a=2; a=2;
``````

According to OP, array 18 has "five elements" even though 17 a.length==12. Even though a===undefined, it 16 is not an element of the array by his definition, and should not be included.

`a.filter(function(x){return true})` will turn 15 the above array into `[1, undefined, undefined, 2, 2]`.

edit: This was originally 14 written with `.reduce()` rather than `.forEach()`, but the `.forEach()` version 13 is much less likely to introduce garbage-collector 12 and pass-by-value issues on inefficient 11 implements of javascript.

For those concerned 10 about compatibility with the 6-year-old 9 MIE8 browser, which does not support the 8 last two editions of the ECMAScript standard 7 (and isn't even fully compliant with the 6 one before that), you can include the code 5 at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach However if one is that concerned about 4 browser compatibility, one ought to program 3 via a cross-compiler like GWT. If you use 2 jQuery, you can also rewrite the above with 1 only a few extra characters, like `\$.forEach(array, ...)`.

Score: 3

For a start, I'm not entirely certain your 9 original code is kosher. It appears to me 8 that it may not work well when the original 7 list is empty, since you try to push the 6 last element no matter what. It may be better 5 written as:

``````var out = [];
var len = arr.length - 1;
if (len >= 0) {
for (var i = 0;i < len; i++) {
if (arr[i] !== arr[i+1]) {
out.push (arr[i]);
}
}
out.push (arr[len]);
}
``````

As to your actual question, I'll 4 answer this as an algorithm since I don't 3 know a lot of JavaScript, but it seems to 2 me you can just remember the last transferred 1 number, something like:

``````# Set up output array.

out = []

# Set up flag indicating first entry, and value of last added entry.

first = true
last = 0

for i = 0 to arr.length-1:
# Totally ignore undefined entries (however you define that).

if arr[i] is defined:
if first:
# For first defined entry in list, add and store it, flag non-first.

out.push (arr[i])
last = arr[i]
first = false
else:
# Otherwise only store if different to last (and save as well).

if arr[i] != last:
out.push (arr[i])
last = arr[i]
``````
Score: 2

Perhaps something like this:

``````var out = [],
prev;

for(var i = 0; i < arr.length; i++) {
if (!(i in arr))
continue;

if (arr[i] !== prev || out.length === 0) {
out.push(arr[i]);
prev = arr[i];
}
}
``````

The `out.length` check is 8 to allow for the first defined array element 7 having a value of `undefined` when `prev` also starts out 6 initially as `undefined`.

Note that unlike your original 5 algorithm, if `arr` is empty this will not push 4 an undefined value into your `out` array.

Or if 3 you have a new enough browser, you could 2 use the `Array.forEach()` method, which iterates only over array 1 elements that have been assigned a value.

Score: 1

I think this is what you want. It's a pretty 1 simple algorithm.

``````var out = [], previous;
for(var i = 0; i < arr.length; i++) {
var current = arr[i];
if(!(i in arr)) continue;
if(current !== previous) out.push(current);
previous = arr[i];
}
``````

This will run in `O(N)` time.

Score: 1

An explicit way would be to pack the array 2 (remove the `undefined`) values and use your existing algorithm 1 for the duplicates on that..

``````function pack(_array){
var temp = [],
undefined;
for (i=0, len = _array.length; i< len; i++){
if (_array[i] !== undefined){
temp.push(_array[i]);
}
}
return temp;
}
``````
Score: 1

A very simple function, the input array 8 must be sorted:

``````function removeDupes(arr) {
var i = arr.length - 1;
var o;
var undefined = void 0;

while (i > 0) {
o = arr[i];

// Remove elided or missing members, but not those with a
// value of undefined
if (o == arr[--i] || !(i in arr)) {
arr.splice(i, 1);
}
}
return arr;
}
``````

It can probably be more concise, but 7 might become obfuscated. Incidentally, the 6 input array is modified so it doesn't need 5 to return anything but it's probably more 4 convenient if it does.

Here's a forward looping 3 version:

``````function removeDupes2(arr) {
var noDupes = [],
o;

for (var i=0, j=0, iLen=arr.length; i<iLen; i++) {
o = arr[i];
if (o != noDupes[j] && i in arr) {
noDupes.push(o);
j = noDupes.length - 1;
}
}
return noDupes;
}
``````

### PS

Should work on any browser that 2 supports javascript, without any additional 1 libraries or patches.

Score: 1

This solution removes duplicates elements 2 in-place. not recommended for functional 1 programming

``````const arr =[0,0,1,1,2,2,2,3,4,5,5,6,7,7,8,9,9,9];

const removeDuplicates = (nums) => {
nums.forEach((element, idx) => {
nums.splice(idx, nums.lastIndexOf(element) - idx)
})
}

removeDuplicates(arr)

console.log(arr);``````

Stay blessed and happy coding!

Score: 0

I believe what you are trying to achieve 18 is not quite possible, but I could be wrong.

It's 17 like one of those classic CS problems like 16 the one where a barber in a village only 15 shaves the one who don't shave themselves. If 14 you set the value of an array's index item 13 as `undefined`, it's not really `undefined`. Isn't that the case? A 12 value can only be `undefined` when it hasn't been initialized.

What 11 you should be checking for is whether a 10 value is `null` or `undefined`. If `null` or duplicate skip the 9 value, else retain it.

If `null` values and duplicates 8 are what you are trying to skip then below 7 function will do the trick.

``````function  removeDuplicateAndNull(array){

if(array.length==0)
return [];

var processed = [], previous=array;
processed.push(array);

for(var i = 1; i < array.length; i++) {

var value = array[i];

if( typeof value !== 'undefined' && value ==null)
continue;

if(value !== previous || typeof value === 'undefined')
processed.push(value);

previous = array[i];
}
return processed;
}
``````

Test cases:

1. `array=[,5,5,6,null,7,7] output =[ ,5,6,7]`

2. `array=[ 5,5,,6,null,,7,7] output=[5,,6,,7]`

3. `array=[7,7,,] output=[7,]`

But 6 even with this function there's a caveat. IF 5 you check third test, the output is [7,] instead 4 of [7,,] ! If you check the length of the input 3 and output arrays, array.length =3 and output.length=2. The caveat 2 is not with the function but with JavaScript 1 itself.

Score: 0

OK I hope this is not a duplicate but lets 3 suppose that you have a sorted array and 2 you can't use additional array to find and 1 delete duplicates:

In Python

``````def findDup(arr, index=1, _index=0):

if index >= len(arr):
return

if arr[index] != arr[_index]:

findDup(arr, index+1, _index+1)

if arr[index] == arr[_index]:
arr = deletedup(arr, index)
findDup(arr, index, _index) #Has to remain same here, because length has changed now

def deletedup(arr, del_index):
del arr[del_index]
return arr

arr = [1, 2, 3, 4, 4, 4, 5, 6, 7, 7, 7, 7, 7]

findDup(arr)
print arr
``````
Score: 0
``````//sort the array
B.sort(function(a,b){ return a  - b});
//removing duplicate characters
for(var i=0;i < B.length; i ++){
if(B[i]==B[i + 1])
B.splice(i,1)
}
``````

if element in next index and current position 2 is same remove the element at current 1 position

``````splice(targetPosition,noOfElementsToBeRemoved)
``````
Score: 0

This code is written in javascript. Its very simple.

Code:

``````function remove_duplicates(arr) {
newArr = [];
if (arr.length - 1 >= 0) {
for (i = 0; i < arr.length - 1; i++) {
// if current element is not equal to next
// element then store that current element
if (arr[i] !== arr[i + 1]) {
newArr.push(arr[i]);
}
}
newArr.push(arr[arr.length - 1]);
}
return newArr
}
arr=[0,1,1,2,2,3,4,5,5,6,7,7,8,9,9,9];
console.log(remove_duplicates(arr));
``````

0

Score: 0

Here is the simple JavaScript solution without 1 using any extra space.

``````function removeDuplicates(A) {
let i = 0;
let j = i + 1;
while (i < A.length && j < A.length) {
if (A[i] === A[j]) {
A.splice(i, 1);
j=i+1;
} else {
i++;
j++;
}
}
return A;
}
console.log('result', removeDuplicates([0,1,1,2,2,2,2,3,4,5,6,6,7]))
``````
Score: 0

You can try the simple way

``````function hello(a: [], b: []) {
return [...a, ...b];
}
let arr = removeDuplicates(hello([1, 3, 7], [1, 5, 10]));
arr = removeDuplicates(arr);
function removeDuplicates(array) {
return array.filter((a, b) => array.indexOf(a) === b);
}
let mainarr = arr.sort((a, b) => parseInt(a) - parseInt(b));
console.log(mainarr); //1,3,5,7,10
``````

One liner code

``````[1,3,7,1,5,10].filter((a, b) => [1,3,7,1,5,10].indexOf(a) === b).sort((a, b) => parseInt(a) - parseInt(b))
``````

0

Score: 0

Here is simple solution to remove duplicates 1 from sorted array.

## Time Complexity O(n)

``````function removeDuplicate(arr) {
let i=0;
let newArr= [];
while(i < arr.length) {
if(arr[i] < arr[i+1]) {
newArr.push(arr[i])
} else if (i === (arr.length-1)) {
newArr.push(arr[i])
}
i++;
}
return newArr;
}
var arr = [1,2,3,4,4,5,5,5,6,7,7]
console.log(removeDuplicate(arr))``````

More Related questions