0

I have the following jQuery script:

var greetingsArray = [
    "Hello, Debbie!",
    "Hello, Randy!",
    "Hello, Carl!" ,
    "Hello, Patrick!",
    "Hello, Susan!",
    "Hello, Susan!",
    "Hello, Carl!"
];

$.uniqueSort(greetingsArray);

Is there a way to get the items that were removed from the array? If so, how would it be used to display a message showing these values?

For example, I have the following HTML:

<div id="greetings"></div>

I want to display removed duplicate greetings in that div element with the following jQuery script:

var greetingsRemoved = ...;
$("#id").html("Removed the following duplicate greetings: " + greetingsRemoved);

What can I put in place of ... to make that happen? I hope this is neither too difficult nor complicated.

Brie
  • 66
  • 6
  • Of course, though... you'll have to implement your own jQuery.uniqueSort method. – Kevin B Nov 04 '16 at 21:43
  • 3
    though... are you sure that method actually works for you? it supposedly doesn't work on arrays of strings/numbers... only arrays of dom elements. – Kevin B Nov 04 '16 at 21:44
  • There are many SO questions about finding all the elements in an array that are duplicated. Use one of those methods to fill in `greetingsRemoved`. – Barmar Nov 04 '16 at 22:20

1 Answers1

1

Although it is probably a bad thing to use jQuery.uniqueSort() for doing this because of the reason stated in its description within the API Documentation, for some reason, it works as intended as follows:

Just testing: Do not use this example!

var greetingsArray = [
    "Hello, Debbie!",
    "Hello, Randy!",
    "Hello, Carl!" ,
    "Hello, Patrick!",
    "Hello, Susan!",
    "Hello, Susan!",
    "Hello, Carl!"
];

var sortedArray = greetingsArray.slice().sort();
var greetingsDuplicate = [];

for (var i = 0; i < greetingsArray.length - 1; i++) {
 if (sortedArray[i + 1] == sortedArray[i]) {
  greetingsDuplicate.push(sortedArray[i]);
 }
}

$("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", "));

$.uniqueSort(greetingsArray);

$("#unique").html("Unique Greetings: " + greetingsArray.sort().join(", "));
<div id="unique"></div>
<div id="duplicate"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

However, there is a better way to do this by implementing the methods derived from both answers provided here and here instead to more accurately display the results you desire.

The Actual Answer

    var greetingsArray = [
        "Hello, Debbie!",
        "Hello, Randy!",
        "Hello, Carl!" ,
        "Hello, Patrick!",
        "Hello, Susan!",
        "Hello, Susan!",
        "Hello, Carl!"
    ];
    
    var greetingsUnique = [];
    
    $.each(greetingsArray, function(index, value){
        if($.inArray(value, greetingsUnique) === -1) greetingsUnique.push(value);
    });
    
    $("#unique").html("Unique Greetings: " + greetingsUnique.sort().join(", "));
    
    var sortedArray = greetingsArray.slice().sort();
    var greetingsDuplicate = [];
    
    for (var i = 0; i < greetingsArray.length - 1; i++) {
     if (sortedArray[i + 1] == sortedArray[i]) {
      greetingsDuplicate.push(sortedArray[i]);
     }
    }
    
    $("#duplicate").html("Duplicate Greetings: " + greetingsDuplicate.join(", "));
<div id="unique"></div>
<div id="duplicate"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Community
  • 1
  • 1
Prince Deekay
  • 382
  • 5
  • 16