Objective
In general, we may give the array weight, this operation is not complex, the implementation of a loop is. Now, what I'm going to do is to determine if there are duplicates in the array, and if so, return True or false.
Ideas
To turn an array into a string
Loop the original array, take each field and this string to compare to see if there are duplicates
How do I compare a string with a B string and ask to determine if the B string contains a string of a.
Method a indexOf () and LastIndexOf () contrast method.
First, we build the code:
var arr = ["AA", "BB", "cc", "BB", "AA"];
Arrrepeat (arr);
As above, we're going to use a arrrepeat (ARR) validation function and execute it, and build this function here
function Arrrepeat (arr) {
var arrstr = json.stringify (arr), str;
for (var i = 0; i < arr.length i++) {
if (Arrstr.indexof (arr[i)!= arrstr.lastindexof (arr[i))) {return
true ;
}
};
return false;
}
OK, run successfully.
The principle is particularly simple, that is, the field in the array, in the string into the array of the first occurrence of the position and the last occurrence of the position is consistent, if not consistent, it shows that the repetition occurred.
Method two match () regular Contrast method
First, as with the above, we build the code:
var arr = ["AA", "BB", "cc", "BB", "AA"];
Arrrepeat (arr);
We then reconstruct the arrrepeat (arr) function
function Arrrepeat (arr) {
var arrstr = json.stringify (arr), str;
for (var i = 0; i < arr.length i++) {
if (Arrstr.match (new RegExp (Arr[i), "G"). Length) >1) {return
True;
}
};
return false;
}
The principle is to find a certain number of repetitions, if it is greater than 1, it must be repeated. Note that here is able to accurately find out how many times the Oh! So, this method actually has a wider use.
OK, run another success
Summarize
If only the first method is actually enough.
The second method can find the actual number of occurrences, such as repeating 4 times, to find 4. The specific use of their own thinking slightly.
Building a regular method that contains a variable new REGEXP (Arr[i], "G") is also asked by others.
In fact, I thought first is the second idea, the problem of the regular problems for half a day, finally resolved. Just think of the first idea.
The above is a small set of JavaScript to tell you how to judge the array of repeated content of the two methods (recommended), I hope to help everyone!