// find the number of duplicate elements in the array (answer 8)

// here are the sketches

<script> a=[1,11,13,121,13,11,11,11,7,9,9] n=a.length cnt=0; for(i=0;i<n-1;i++){ flag=true; for(j=i+1;j<n;j++){ if((a[i]==a[j])&&(i==j)) flag=false; } if(flag){cnt++}; } console.log(cnt) </script> 

// correct my entries if possible

  • Need to find the total number or amount of one maximally repeating element? What is the correct answer in your example? - Kromster
  • @KromStern, the answer is 8, so you need the number of all non-unique elements in the array. - Qwertiy
  • @Qwertiy count as follows (11,13,13,11,11,11,9,9) total 8 - cheburashkarf
  • cheburashkarf, I thought so. This for @KromStern was the answer. - Qwertiy

3 answers 3

Classic way

 function calc(a) { var count={}, res=0, q; for (q=0; q<a.length; ++q) { count[a[q]] = ~~count[a[q]] + 1; } for (q in count) { if (count.hasOwnProperty(q) && count[q] > 1) { res += count[q]; } } return res; } calc([1,11,13,121,13,11,11,11,7,9,9]) // 8 

ES6

 function calc(a) { let count = Object.create(null); for (let x of a) { count[x] = ~~count[x] + 1; } return a.length - Object.keys(count).filter(x => count[x]>1).length; } calc([1,11,13,121,13,11,11,11,7,9,9]) // 8 

PS: I admit that you can still rewrite something on ES6.

  • one
    Sorry, but how this game ~~ count [a [q]] + 1; Can I rewrite it more clearly? - cheburashkarf
  • in general, it works like rounding down, but where is it? - vihtor
  • Otherwise, this line can be represented as follows: count [a [q]] = a [q] in count? count [a [q]] + 1: 1; - vihtor 7:42 pm
  • @vihtor, this is a forced cast in int32. Including turns undefined, NaN and any garbage to 0. Your replacement option is basically correct. If nobody in Object's prototype cram numeric keys. However, if there are numeric keys with numeric values, mine will not help either. But this situation is generally from the realm of fantasy. - Qwertiy
  • @cheburashkarf, if (count[a[q]]) { count[a[q]] = 1; } else { ++count[a[q]]; } if (count[a[q]]) { count[a[q]] = 1; } else { ++count[a[q]]; } if (count[a[q]]) { count[a[q]] = 1; } else { ++count[a[q]]; } . With the same drawbacks as listed above. - Qwertiy

I did it

 function countDoubles(list) { var items = list.slice(0), // клонируем исходный массив tested = [], // список проверенных элементов item, // один элемент count = 0; // кол-во элементов, имеющх дубли while (items.length) { // вырезаем первый элемент (что бы он нам больше не попадался) item = items.shift(); // если его еще не проверяли if (tested.indexOf(item) === -1) { // добавляем в список tested.push(item); // и ищем дубли if (items.indexOf(item) >= 0) { count++; } } } return count; } countDoubles([1, 1, 2, 2, 3, 3, 4, 5, 3, 0, 0, 0]) // 4 countDoubles([1,11,13,121,13,11,11,11,7,9,9]) // 3 

    There is such a cool library linqjs.

     Enumerable.From([1,11,13,121,13,11,11,11,7,9,9]).Distinct().ToArray() 

    Will return unique knowledge.

    In addition, there are a lot of other usefulness. Link http://neue.cc/reference.htm


    lodash / underscore

     _.uniq([1,11,13,121,13,11,11,11,7,9,9]) 

    You get unique values. There are also a lot of buns, such as checking, working with arrays.

    If you are not sure about your knowledge, use the frameworks, they have already done everything for you there.

    Good luck!

    • And here is a list of unique elements? The condition states: the number of repeating elements - vihtor
    • oh yes :) I didn’t read something) Quarrel. - sbaet
    • Then in our example it would be 'Enumerable.From ([1,11,13,121,13,11,11,11,7,9,9]). Distinct (). Count ()' -------- --- _.uniq ([1,11,13,121,13,11,11,11,7,9]]. length - sbaet
    • Well, so you again choose a list of unique items. but according to such a list it is impossible to say which elements were repeated. for example: [1,2, 3, 3] in your decision, the result will be 3 (three unique elements), but only one of them is repeated, so the correct answer will be 1 - vihtor
    • one
      Although no, the condition states that the correct answer is 8, which means that not only the repetitions but also the first number are taken into account. So in my example, the correct answer is 4, because [1,2, 3 , 3 , 3 , 3 ] - vihtor