Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»JavaScript Program to Remove Duplicate Array Elements

    JavaScript Program to Remove Duplicate Array Elements

    By RahulJuly 20, 20211 Min Read

    An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string but in the case of JavaScript, we can store different types of elements. Using arrays you can organize data so that a related set of values can be easily sorted or searched.

    Advertisement

    This tutorial described to you to remove duplicate array elements using JavaScript.

    Example

    Here is a sample JavaScript program to declare a static array with few default elements. Add some duplicate elements as well. After that, remove all the duplicate array elements.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // An example JavaScript program to remove duplicate values from an array
     
    function returnUnique(value, index, self) {
      return self.indexOf(value) === index;
    }
     
    // usage example:
    var arr = ['Green', 'Red', 100, 'Green', 20, '100'];
    var unique = arr.filter(returnUnique);
     
    console.log(unique); //

    Output:

    ["Green", "Red", 100, 20, "100"]
    

    In the above example, the value “Green” was defined twice in the array. As a result, you see only one instance of Green.

    You may be thinking about element 100. Here the first occurrence of the element 100 is an integer value and the second one is defined as a string. In short, both elements are treated as unique.

    array javascript
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to list all collections in MongoDB database

    What is difference between var, let and const in JavaScript?

    What is difference between var, let and const in JavaScript?

    How do I Replace String in JavaScript

    How to Replace String in JavaScript

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.