-4

Once removed duplicates, find the length of an array. I'm using the below code but it's returning 0 as the length of the array:

var mydata = ["1", "2", "3", "3", "4", "5", "5", "6", "7", "7", "8", "9", "9"];

var uniqueNames = [];
$(function() {
  $.each(mydata, function(i, el) {
    if ($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
  });
})
console.log(uniqueNames.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Total length

13

Expected output

9
Ashok P
  • 35
  • 1
  • 9

3 Answers3

3

Using modern JS, getting the Dups can be done just using a set..

eg..

const myData = ["1","2","3","3","4","5","5","6","7","7","8","9","9"];

const uniqueData = Array.from(new Set(myData));

console.log(myData.join(", "));
console.log(uniqueData.join(", "));
console.log(`length = ${uniqueData.length}`);
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Again, this answer completely misses what the *actual issue* was. See my other comments. –  Jun 13 '18 at 08:53
  • @ChrisG The answer doesn't always have to answer the OP's specific problem, if the post could be deemed useful, it's useful. And because the question even has a title with duplicates, it has a good chance of been found from search. It's great your modding everyone's post's, but please learn to moderate with some common sense. – Keith Jun 13 '18 at 09:03
  • If you're all about the big picture, why didn't you flag this as duplicate then? https://stackoverflow.com/questions/1960473/get-all-unique-values-in-an-array-remove-duplicates https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array –  Jun 13 '18 at 10:26
  • @ChrisG https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – Keith Jun 13 '18 at 13:41
  • @ChrisG Not sure of the `but`, but at least you now know this answer was not an issue, that's the main. Just a note, you answered a question today, that had a much better answer on SO, is this the point your making?. Anyway, in the spirit of SO, lets just keep on helping people shall we, really don't want to get into a tit for tat with you. – Keith Jun 13 '18 at 14:24
  • Do you mean the weekdays thing? Yeah, I didn't write golf code for that one on purpose so the beginner OP was hopefully able to see what I did and why without a lengthy explanation. I was actually hesitant to post it but decided to do it anyway. Doesn't matter; my original point stands: your answer doesn't address the OP's issue and is therefore "not useful" in the context of this question. And since it's very unlikely that somebody a) searches SO for "how to determine array's length" *and* b) doesn't immediately find the 819 votes answer, this question doesn't serve any other purpose. –  Jun 13 '18 at 14:37
  • @ChrisG No, I meant there was already a duplicate on your answer on SO, that was better than yours. So your answer served no purpose, the duplicate would have been fine. `doesn't serve any other purpose.` 3 up-votes proves that incorrect. – Keith Jun 13 '18 at 14:45
  • Please link me to that. And those 3 upvotes only prove that at least three people besides you didn't properly read the OP's question. –  Jun 13 '18 at 15:14
  • `Please link me to that.` you not know how to use SO search?.. `read the OP's question.` There is no way of you knowing why they up-voted, stop been silly. – Keith Jun 13 '18 at 15:16
  • If I have a useless answer on SO, I want to remove it. I don't have any idea which answer or question you're referencing, so wouldn't even know what to search for. PLEASE link me to my useless answer. –  Jun 13 '18 at 15:18
3

The issue is because you're calling console.log() outside the jQuery document.ready event handler, hence you're logging the length before you've de-duped the array. Put the length check inside the handler:

var mydata = ["1", "2", "3", "3", "4", "5", "5", "6", "7", "7", "8", "9", "9"];

var uniqueNames = [];
$(function() {
  $.each(mydata, function(i, el) {
    if ($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
  });

  console.log(uniqueNames.length);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

That being said, you don't need jQuery at all here. jQuery is intended to be used as a DOM manipulation framework. Native JS methods are more effective for working with arrays:

var mydata = ["1", "2", "3", "3", "4", "5", "5", "6", "7", "7", "8", "9", "9"];

var uniqueNames = mydata.filter((value, index, arr) => {
  return arr.indexOf(value) === index; 
});

console.log(uniqueNames.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You can check lastIndexOf() of the element so that this last index match the current index. If so, push that element in uniqueNames array. Then you can also get the length of it using uniqueNames.length

var myData = ["1","2","3","3","4","5","5","6","7","7","8","9","9"];
var uniqueNames = [];
myData.forEach((num, index)=>{
  if(myData.lastIndexOf(num) === index){
    uniqueNames.push(num);
  }
});
console.log(uniqueNames);
console.log('length is '+ uniqueNames.length);

USING filter()

var myData = ["1","2","3","3","4","5","5","6","7","7","8","9","9"];
var uniqueNames =  myData.filter((num, index)=> myData.lastIndexOf(num) === index);
console.log(uniqueNames);
console.log('length is '+ uniqueNames.length);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62