-3

for(var i=0;i<d.length;i++)
{
  current=d[i].businessunits;
  arraynew.push(current);//array new like this {0:EU;1:PU;2:MS;3:EU;4:PU}
}

I have an array var k={"Eu","PU","MS","EU","PU"} and I need only EU PU MS because EU and PU are Repeating.How to remove the Repeating Elements in an array.

khakishoiab
  • 9,673
  • 2
  • 16
  • 22
SHERIN AS
  • 93
  • 7
  • 3
    Possible duplicate of [Unique values in an array](http://stackoverflow.com/questions/1960473/unique-values-in-an-array) – A.T. Sep 16 '16 at 05:55

8 Answers8

2

Check element already exist in an array before pushing the value.

for(var i=0;i<d.length;i++)
{
  current=d[i].businessunits;
  if(arraynew.indexOf(current) === -1) arraynew.push(current);//array new like this {0:EU;1:PU;2:MS;3:EU;4:PU}
}
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

With ES6, you could use Set, because set keeps only one element of the same.

Solution for all upper case.

var k = ["EU", "PU", "MS", "EU", "PU"];

k = [... new Set(k)];
console.log(k);

Solution for mixed case.

var k = ["Eu", "PU", "MS", "EU", "PU"];

k = [... new Set(k.map(a => a.toUpperCase()))];
console.log(k);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    OP does not want "Eu" – Flying Gambit Sep 16 '16 at 06:02
  • 2
    @FlyingGambit he does not want duplicate elements. Also @ Nina, you should add caveat for ES6. If OP is using ES5, it wont work. – Rajesh Sep 16 '16 at 06:15
  • 2
    @Rajesh OP's example array was ["Eu","PU","MS","EU","PU"] which includes "Eu". OP wants only ["EU", "PU", "MS"]. This answer is not a solution to what OP wants as he is looking for a case insensitive removal of duplicates. Also Nina changed his array elements , check the edits – Flying Gambit Sep 16 '16 at 06:32
  • @FlyingGambit : I don't think so... the array is like `{0:EU;1:PU;2:MS;3:EU;4:PU}` ................... and ` var k={"Eu","PU","MS","EU","PU"}` is not a valid js – Pranav C Balan Sep 16 '16 at 06:38
1

work on all browsers:

myArray.filter(function (x, i, a) { 
    return a.indexOf(x) == i; 
});

for modren browsers

[...new Set(myArray)]
A.T.
  • 24,694
  • 8
  • 47
  • 65
0

Hashtables are a good tool for this. Here is some pseudocode:

  1. Iterate over the array and add each item into the hashtable as a key if it doesn't already exist
  2. Return a list of the keys in the hashtable.

If you are using a language which supports sets, it is even easier- simply update the set once for each element in the array.

puzzled123
  • 312
  • 1
  • 2
  • 12
0

Simple solution using Array.forEach and Array.indexOf functions:

var arr = ["Eu","PU","MS","EU","PU"], result = [];

arr.forEach(function(v){
    result.indexOf(v.toUpperCase()) === -1 && result.push(v.toUpperCase());
});

console.log(result);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Following code check before adding element in new array so. new array not contains repeating elements

for(var i=0;i<d.length;i++)
{
   current=d[i].businessunits;
   if(arraynew.indexOf(current) === -1) // if new array not contains element add in new array 
      arraynew.push(current);
}
Farhan Yaseen
  • 2,507
  • 2
  • 22
  • 37
0

You can sort the array and push non matching elements.

Sort + array.reduce

var a = ["EU", "PU", "MS", "EU", "PU"];
var r = [];
a.sort().reduce(function(p,c){
  if(p!==c) r.push(c)
  return c;
}, '');

console.log(r)

Sort + for

var a = ["EU", "PU", "MS", "EU", "PU"];
var r = [a[0]];
a.sort()

for(var i = 0; i<a.length -2; i++){
    if(a[i] !== a[i+1]) r.push(a[i+1])
}
console.log(r)
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

Easy solution

var checkDuplicateObject = {},arraynew = [];
for(var i=0;i<d.length;i++)
{
   if(checkDuplicateObject(d[i].businessunits)){
   }else{
    var current = d[i].businessunits;
    arraynew.push(current);  
    checkDuplicateObject[d[i].businessunits] = true;
  }
}
Aatif Bandey
  • 1,193
  • 11
  • 14