Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Remove Duplicate Items from Array

Contributor ,
Apr 23, 2021 Apr 23, 2021

I frecuently receive information from customers (image below)
I need to remove duplicate numbers

How can I create a new array without duplicated items
any help will be appreciate

GerssonDelgado_0-1619206954657.png

 

TOPICS
Scripting , SDK
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Guide , Apr 23, 2021 Apr 23, 2021

 

function removeDuplicate(a) {
    for (var i = 0; i < a.length; i++) {
        for (var j = 0; j < a.length; j++) {
            if (i != j && a[i] == a[j]) {
                a.splice(j, 1);
            }
        }
    }
    return a;
}

var array1 = [1, 1, 1, 2, 2, 2, 3, 3, 3];
var array2 = removeDuplicate(array1);
alert( array2 );  // 1,2,3

 

Translate
Community Expert , Apr 24, 2021 Apr 24, 2021

I usually make a "map", meaning adding each array value to an object. Since you can't have duplicate object keys, each duplicate value would replace it's previous value if it already exists

 

var map = {};

var arr = [1,2,3,1,2,3];

for (var a=0; a<arr.length; a++) {
    map[arr[a]] = arr[a]; // {"1":1, "2":2, "3":3}
}

var unique = [];

for (var b in map) {
    unique.push(map[b]);
}

alert(unique);
Translate
Adobe
Guide ,
Apr 23, 2021 Apr 23, 2021

 

function removeDuplicate(a) {
    for (var i = 0; i < a.length; i++) {
        for (var j = 0; j < a.length; j++) {
            if (i != j && a[i] == a[j]) {
                a.splice(j, 1);
            }
        }
    }
    return a;
}

var array1 = [1, 1, 1, 2, 2, 2, 3, 3, 3];
var array2 = removeDuplicate(array1);
alert( array2 );  // 1,2,3

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 23, 2021 Apr 23, 2021

Awesome! Thanks in advance !!!

 

Gdelgado

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2021 Apr 23, 2021

sorry I don't mean harm but this array breaks the algo

 

var array1 = [2, 3, 1, 2, 2, 1, 3, 3, 2]; // returns 3,1,2,3

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 24, 2021 Apr 24, 2021

Oops. Thanks for the catch.

 

What about

 

function removeDuplicate(a) {
    for (var i = a.length-1; i > -1; i--) {
        for (var j = 0; j < a.length; j++) {
            if (i != j && a[i] == a[j]) {
                a.splice(j, 1);
            }
        }
    }
    return a;
}
var array1 = [2, 3, 1, 2, 2, 1, 3, 3, 2];
var array2 = removeDuplicate(array1);
alert( array2 );  // 1,3,2

 

 

Alternatively

 

function removeDuplicate(a){
    var b = [];
    for (var i = 0; i < a.length; i++) {
        var duplicate = (function () {
            for (var j = 0; j < b.length; j++) {
                if (b[j] == a[i]) {
                    return true;
                }
            }
            return false;
        } () );
        if (!duplicate) {
            b.push(a[i])
        }
    }
    return b;
}
var array1 = [2, 3, 1, 2, 2, 1, 3, 3, 2];
var array2 = removeDuplicate(array1);
alert( array2 );  // 2,3,1

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2021 Apr 24, 2021

both versions work!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2021 Apr 24, 2021

I usually make a "map", meaning adding each array value to an object. Since you can't have duplicate object keys, each duplicate value would replace it's previous value if it already exists

 

var map = {};

var arr = [1,2,3,1,2,3];

for (var a=0; a<arr.length; a++) {
    map[arr[a]] = arr[a]; // {"1":1, "2":2, "3":3}
}

var unique = [];

for (var b in map) {
    unique.push(map[b]);
}

alert(unique);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 26, 2021 Apr 26, 2021

this is my approach as well. feels nice and tidy.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 26, 2021 Apr 26, 2021
LATEST

Thank you all for your support!! 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines