Check If Element Is In Array Twice Time
I need to know if an element is in array twice or many time. var arr = [elm1,elm2,elm3,elm3,elm4,elm5,elm5,elm5,elm6,elm7] ; if(elm is in array by many time ){ // do some cod
Solution 1:
The countInArray function may be an option for you
functioncountInArray(array, what) {
returnarray.filter(item => item == what).length;
}
Or something like this, this may be better for you to understand the code and adjust somethings where you want to! :
varlist = [2, 1, 4, 2, 1, 1, 4, 5];
functioncountInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
countInArray(list, 2); // returns 2
countInArray(list, 1); // returns 3
Solution 2:
functioncheck(){
var arr =['1','2','3','3','4'];
for (i=0; i<arr.length;i++){
for (x=0;x<arr.length;x++){
if(arr[i]==arr[x] && i != x){
console.log('SAME ones in ARRAY: '+arr[i]);
}elseconsole.log('no same ones');
}
}
}
Post a Comment for "Check If Element Is In Array Twice Time"