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

8
  • Possible duplicate of How to remove a particular element from an array in JavaScript? Commented Sep 20, 2016 at 8:35
  • @GillBates : I have updated my question. Can you check now please. Commented Sep 20, 2016 at 8:37
  • Still the same question, just use array.indexOf("a") and then array.splice(index, 1); Commented Sep 20, 2016 at 8: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) . Commented Sep 20, 2016 at 8:41
  • No, the 1 states "remove only 1". Commented Sep 20, 2016 at 8:42

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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"]));

4 Comments

This is the solution what i was expecting. Thanks
The above code is not working for NodeJs. May be i am missing correct syntax.
please try the second proposal.
please try the third one.
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);

2 Comments

can we customize above function to automatically detect all duplicates ,basically i can not send one parameter , it can be anything.
can try second process without passing any parameters @ShashikantSharma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.