Home>

Tell me how to iterate through the array to replace duplicates with empty strings. You need to leave one unique value each, and replace duplicates with an empty string.

let arr= [1, 2, 3, 1, 3, 1, 5];

In this example array, it should look like this:

let arr= [1, 2, 3, '', '', '', 5];

use an additional array and the Includes method, which will check for the presence of this value in the existing array (where the values ​​\u200b\u200bwill be "shifted" to)

Алексей Шиманский2022-02-12 23:21:15

@AlekseyShymansky is better Set

Alexey Ten2022-02-12 23:21:15

@Alexey Ten can you show an example with Set?

Александр2022-02-12 23:21:15

@Alexander wouldn't it be better to just remove the duplicates?

Проста Miha2022-02-12 23:21:15

Here's a one-liner res=arr.map(function(v){return this.has(v)?'':(this.add(v),v)},new Set) ????

Alexey Ten2022-02-12 23:21:15
  • Answer # 1

    For an efficient replacement (in linear time), you should useset:

    In-place replacement:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var used= new Set()
    for (var q=0; q<a.length; ++q) {
      if (used.has(a[q])) {
        a[q]= ""
      } else {
        used.add(a[q])
      }
    }
    console.log(a)

    With the creation of a new array:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var used= new Set()
    varb= a.map(x=> used.has(x) ? "" : (used.add(x), x))
    console.log(b)

    And another cool trick as a bonus:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var i= 0
    for (var x of new Set(a)) {
      while (a[i++] !== x) {
        a[i-1]= ""
      }
    }
    for (; i < a.length; ++i) {
      a[i]= ""
    }
    console.log(a)
  • Answer # 2

    For an efficient replacement (in linear time), you should useset:

    In-place replacement:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var used= new Set()
    for (var q=0; q<a.length; ++q) {
      if (used.has(a[q])) {
        a[q]= ""
      } else {
        used.add(a[q])
      }
    }
    console.log(a)

    With the creation of a new array:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var used= new Set()
    varb= a.map(x=> used.has(x) ? "" : (used.add(x), x))
    console.log(b)

    And another cool trick as a bonus:

    var a= [1, 2, 3, 1, 3, 1, 5]
    var i= 0
    for (var x of new Set(a)) {
      while (a[i++] !== x) {
        a[i-1]= ""
      }
    }
    for (; i < a.length; ++i) {
      a[i]= ""
    }
    console.log(a)
  • Answer # 3

    Here is the variant withsetand without quadratic complexity:

    const arr= [1, 2, 3, 1, 3, 1, 5];
    const seen= new Set();
    const res= arr.map((val)=> {
        if (seen.has(val)) {
            return '';
        }
        seen.add(val);
        return value;
    });
    console log(res);
    

    Oh, I almost wrote the same

    Qwertiy2022-02-12 23:21:15
  • Answer # 4

    Here is the variant withsetand without quadratic complexity:

    const arr= [1, 2, 3, 1, 3, 1, 5];
    const seen= new Set();
    const res= arr.map((val)=> {
        if (seen.has(val)) {
            return '';
        }
        seen.add(val);
        return value;
    });
    console log(res);
    

    Oh, I almost wrote the same

    Qwertiy2022-02-12 23:21:15
  • Answer # 5

    Here's my version, I hope it helped

    let arr= [1, 2, 3, 1, 3, 1, 5];
    for(let i= 0; i < arr.length; i++){
      for(let j= i + 1; j < arr.length; j++){
        if(arr[i]== arr[j]) arr[j]= "";
      }
    }
    console.log(arr);
  • Answer # 6
    const arr= [1, 2, 3, 1, 3, 1, 5];
    const res= [];
    for (let i= 0; i < arr.length; i++) {
        if (res.includes(arr[i])) {
        res push('');
      }
      else {
        res push(arr[i]);
      }
    }
    console.log(res);

    Here is a working, but not the best option. I think it's good for you to understand.