• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

Remove Duplicate Array Values in JavaScript

Written by Rahul, Updated on November 27, 2020

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 case of JavaScript, we can store different type of elements. Using arrays you can organize data so that a related set of values can be easily sorted or searched.

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

Example

For example, create an array with some default values with adding duplicate array elements. Then remove all the duplicate array elements using JavaScript.

1
2
3
4
5
6
7
8
9
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, value “Green” was defined twice in the array. As a result, you see only one instance of Green.

You may thinking about the element 100. Let me clear that the first occurrence of the element 100 is an integer value and the second one is defined as string. In short both the elements are treated as unique.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy