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

Remove Duplicate Items from Array

Participant ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

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

Views

911

Translate

Translate

Report

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

 

Votes

Translate

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

Votes

Translate

Translate
Adobe
Guide ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

 

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

 

Votes

Translate

Translate

Report

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
Participant ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Awesome! Thanks in advance !!!

 

Gdelgado

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

both versions work!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Participant ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

LATEST

Thank you all for your support!! 🙂

Votes

Translate

Translate

Report

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