0

I wish to preserve the following (as opposed to the preceding) duplicated substring of a comma-delimited string while removing the preceding duplicating substring.

1- Initial state of string before duplicate appended:

aaa,bbb,ccc,ddd,eee

2- bbb dynamically appended to string:

aaa,bbb,ccc,ddd,eee,bbb

3- Preceding bbb now must be removed:

aaa,ccc,ddd,eee,bbb

How can the following function, or any function for that matter, reproduce what I seek?

function unique(list) {
    var result = [];
    $.each(list, function(i, e) {
    if ($.inArray(e, result) == -1) {
result.push(e);
}
    });
    return result;
}
koolness
  • 166
  • 19
  • what do you want to remove from the string... – valar morghulis Apr 01 '15 at 16:40
  • I think wou may want to read this kind of things : http://stackoverflow.com/questions/1960473/unique-values-in-an-array – Jeremy Thille Apr 01 '15 at 16:43
  • I simply need to remove the preceding duplicate from the string. All examples and links show methods to remove the following duplicate. – koolness Apr 01 '15 at 17:16
  • Check indexOf and lastIndexOf, if both are not equal, then `splice` using IndexOf :) It will remove the first element – mohamedrias Apr 01 '15 at 17:44
  • I've re-opened this question as I don't believe it to be a duplicate of the question it was closed as a duplicate of. This question specifically wants to maintain order and keep the last occurrence of any duplicate. This is different than just removing duplicates. – James Montagne Apr 08 '15 at 20:10

2 Answers2

2

The quick and easy way is to just reverse the array, use your duplicate removal function and then reverse it back:

function unique(list) {
  var result = [];
  $.each(list, function(i, e) {
    if ($.inArray(e, result) == -1) {
      result.push(e);
    }
  });
  return result;
}

var arr = ["aaa", "bbb", "ccc", "ddd", "eee", "bbb"];

console.log(unique(arr.reverse()).reverse());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

There are likely ways to do this with better performance, but with a relatively small array this should be fine.

You also mention that this is a string (though your function works on arrays). If it is a string, you will need to use split to break it into an array and then join after the duplicate removal to get it back to a string.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • Why do we need to reverse it? – mohamedrias Apr 01 '15 at 17:42
  • @mohamedrias The existing function keeps the first duplicate and removes all others. The reverse makes it keep the last duplicate as requested. – James Montagne Apr 01 '15 at 17:44
  • Please check my answer, it removes without that :) – mohamedrias Apr 01 '15 at 17:45
  • @mohamedrias I'm sure there are plenty of ways to do this. This is but one. One thing that should probably be pointed out with yours is that it alters the original array. The original function returns a new array and leaves the original unaltered. – James Montagne Apr 01 '15 at 17:46
  • Ya, its altering the original array. Hope i can just add the element to new Array in if else condition. Else just use [].concat(list)/arr.slice() which will return new array and i can process in that :) But yes, there are many ways of achieving it. :) – mohamedrias Apr 01 '15 at 17:49
  • @mohamedrias Yes, you can certainly reverse your condition and then add to a result array instead of removing from the original. I don't know if the OP cares if the array is altered, I just noticed it as a difference. – James Montagne Apr 01 '15 at 17:55
  • Yeap, I need to keep string in original order. I was experimenting with reverse() when I landed on your answer. Just what I needed! Thx. – koolness Apr 01 '15 at 19:31
0

Your function need to be like this:

function unique(list) {
       $.each(list, function(index, val) {
         if(list.indexOf(val) !== list.lastIndexOf(val)) {
            list.splice(list.indexOf(val),1);          
         }
      });
      return list;
}
var string = "aaa,bbb,ccc,ddd,eee,bbb";
var arr = string.split(",");

document.querySelector("#results").innerHTML = unique(arr).join(",")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="results"></div>

If it matches duplicate, then it can just remove that element.

mohamedrias
  • 18,326
  • 2
  • 38
  • 47