JS Array common functions and three common methods for finding duplicate elements in an array

Source: Internet
Author: User

Read the catalogue:
    • DS01: Three ways to find duplicate elements in a common array
    • DS02: A collection of commonly used JS functions
three ways to find duplicate elements in an array, commonly used in DS01

1.

1   varary =NewArray ("111", "22", "33", "111"); 2   vars = Ary.join (",") + ","; 3    for(vari=0;i<ary.length;i++) {  4       if(S.replace (ary[i]+ ",", ""). IndexOf (ary[i]+ ",") >-1) {  5Alert ("There are repeating elements in the array:" +Ary[i]); 6            Break; 7       }8}

2.

1  var New Array ("111", "a", "" "," 111 ");   2  var nary=Ary.sort ();   3   for (var i=0;i<ary.length;i++) {  4       if (nary[i]==nary[i+1 ] {  5           alert ("There are repeating elements in the array:" +nary[i])  ; 6       }  7  }

3.

1 functionIsrepeat (arr) {2      varhash = {}; 3       for(varIincharr) {   4          if(Hash[arr[i]])5               return true; 6Hash[arr[i]] =true; 7      }   8      return false; 9}

Back to Top

DS02: A collection of commonly used JS functions

1. Pop method
Function: Removes the last element in the array and returns the element.
Example: Arrayobj.pop ()Description:The required arrayobj reference is an Array object.
if the array is empty, then the undefined is returned.   2. Push Method
Function: Adds a new element to an array and returns the new length value of the array.
Example: Arrayobj.push ([item1 [item2 [...] [Itemn]])
Parameters:
Arrayobj, Required option, an Array object.
Item, Item2,... itemn options available. Represents the new element of the Array. return value: The new length value of the array.   3. Unshift Method
Function: Inserts the specified element into the starting position of the array and returns the array.
Example: Arrayobj.unshift ([item1[, item2 [, ...] [, Itemn]]])
Parameters:
Arrayobj, Required option, an Array object.
Item1, item2,.. ., Itemn, optionally, the element that will be inserted into the beginning part of the Array.
Description
The Unshift method inserts these elements into the beginning of an array, so the elements appear in the array in the order in which they are in the parameter sequence.
return value: the current array. 4. Splice Method
Function: Removes one or more elements from an array and, if necessary, inserts a new element at the location of the removed element, returning the element that was removed.
Example: Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ... [, Itemn]]])
Parameters:
Arrayobj, Required option, an Array object.
Start, required option, specifies the starting position of the element to be removed from the array, which is calculated starting at 0.
DeleteCount, required option, the number of elements to remove.
Item1, item2,.. ., itemn, a mandatory option to insert a new element at the location of the removed element.
Description
The splice method modifies the Arrayobj by removing the specified number of elements from the start position and inserting the new element. return value: is a new Array object consisting of the removed elements.

Back to Top

19:38:09 2015-01-15

by Selonsy

(Pending update)

JS Array Common functions and find out if there are duplicate elements in the array three common methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.