By 05/02/2014 13 Comments

How to removing duplicate element from array in Javascript

What is an array ?

  • In javascript it is called objects, it is also called data structures
  • With the help of an array we can store multiple values in a single variable
  • Array stores values in it’s index location
  • Array uses indexes for storing values, that indexes start from 0 (zero) and so on
  • Every value which stored in an array has its specific location
  • The values store in an array are called array elements
  • It is very useful for real web applications, for instance we can store users, products, country names etc
  • By default duplicate elements can be stored in an array

Following code snippet will guide you for removing duplicate elements from an array in Javascript.


function removeDuplicates(arr) { // declare a function, pass an array as an parameter
var temp = {};  // declare empty variable for a temporary storage
for (var i = 0; i < arr.length; i++) // loop through each array elements
temp[arr[i]] = true;

var r = []; // declare an empty array
for (var k in temp) // loop through each array element
r.push(k); // array's push(method), this function adds new item to an array
return r; // return variable for rendering the output

var fruits = ['apple', 'orange', 'peach', 'apple', 'strawberry', 'orange']; // create an array with name fruits with 6 elements, there are orange and apple 2 duplicate elements
var uniquefruits = removeDuplicates(fruits); // call above function and pass an array as a argument to that function
//print uniquefruits ['apple', 'orange', 'peach', 'strawberry']; // this is the output on your browser


Posted in: JavaScript

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .