0

I am looking for a simple solution. I have an array like array("a","b","c","a","g");

Now when i try splice or custom remove function , it removes all "a" elements from array.

What i want is when i remove "a" from the array , then the array should finally have 1 "a" element only inside it and remove all other dulicates of "a".

Now consider another case , when my array is like array("a","a","b","c","a") , then after removing "a" , the array should like array("a","b","c").

Help needed,

Thanks in advance

BeingExpert
  • 373
  • 3
  • 13
  • Possible duplicate of [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript) – Hatted Rooster Sep 20 '16 at 08:35
  • @GillBates : I have updated my question. Can you check now please. – BeingExpert Sep 20 '16 at 08:37
  • Still the same question, just use `array.indexOf("a")` and then `array.splice(index, 1);` – Hatted Rooster Sep 20 '16 at 08:39
  • @GillBates : I think you still getting it wrong or i may be wrong but `splice` remove all same elements from array for me. i have used same `array.splice(index, 1)` . – BeingExpert Sep 20 '16 at 08:41
  • No, the `1` states "remove only 1". – Hatted Rooster Sep 20 '16 at 08:42
  • Why don't you say "i want to remove the duplicates..." and be a member of "a million times answered question asked again club"... Use Set object. – Redu Sep 20 '16 at 08:45
  • Possible duplicate of [Array of objects in js: remove duplicates](http://stackoverflow.com/questions/34566658/array-of-objects-in-js-remove-duplicates) – Redu Sep 20 '16 at 08:47
  • the question remains unclear, do you like to remove only multiple `'a'`? or all duplicate items in the array? – Nina Scholz Sep 20 '16 at 09:01

4 Answers4

0

You could use Set and keep only unique items in the array.

var unique = array => array.filter((set => a => !set.has(a) && set.add(a))(new Set));

console.log(unique(["a", "b", "c", "a", "g"]));
console.log(unique(["a", "a", "b", "c", "a"]));

Different style

var unique = function (array) {
        return array.filter(function (a) {
            if (!this.has(a)) {
                this.add(a);
                return true;
            }
        }, new Set);
    };

console.log(unique(["a", "b", "c", "a", "g"]));
console.log(unique(["a", "a", "b", "c", "a"]));

ES5 with Object as hash

var unique = function (array) {
        return array.filter(function (a) {
            if (!this[a]) {
                return this[a] = true;
            }
        }, Object.create(null));
    };

console.log(unique(["a", "b", "c", "a", "g"]));
console.log(unique(["a", "a", "b", "c", "a"]));
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

You can make use of the filter method like:

var duplicates = ["a","a","b","c","a"];
var withoutDuplicates = duplicates.filter(function(currentValue, index) {
    return duplicates.indexOf(currentValue) == index;
});
console.log(withoutDuplicates); // Array [ "a", "b", "c" ]

As also stated here: Remove Duplicates from JavaScript Array

Documentation: http://www.w3schools.com/jsref/jsref_filter.asp

Michael Troger
  • 2,935
  • 2
  • 18
  • 38
0

Try this snippet

var arr = ["a","a","b","c","a"];
var arrayLength = arr.length;

for (var i = 0; i < arrayLength; i++) {
  for (var j = i + 1; j < arrayLength;) {
    if (arr[j] == arr[i]) {
      for (var k = j; k < arrayLength; k++) {
        arr[k] = arr[k + 1];
      }
      arrayLength--;
    } else
      j++;
  }
}

var newArr = [];
for (var i = 0; i < arrayLength; i++) {
  newArr.push(arr[i]);
}
console.log(newArr);
cheralathan
  • 536
  • 4
  • 14
0

Can try this one

var array = ["a","a","b","c","a"];

var filterIfMultiple = function (data) {
  var count = 0;
  array = array.filter(function(val) {
              if (val === data) count+=1; 
              if(val !== data || (val === data && count <= 1)){
                 return val;
              }
          });
};

filterIfMultiple("a");
console.log(array);

Another process without passing any parameter using ES6 Set object

var array = ["a","a","b","c","a","b"];

function filterIfDuplicate() {
   array = Array.from(new Set(array));
}

filterIfDuplicate();
console.log(array);
Shaishab Roy
  • 14,345
  • 4
  • 44
  • 63