Get value from select2 multiple then shows alert javascript

I have a input with select2 like below

<select class="form-control" name="location" multiple="multiple" id="location"></select>

So I want user to input / type the location, and user can typing it multiple times and each location separated by press enter button

I want to make system, that if user have 10 locations needed to input, when location number 7 input have same value with location number 3 user typed before, it shows alert to make user aware.

Right now, if user typing location number 7 input input have same value with location number 3 user typed before, the data location number 7 is just disappear.

So I want to get value from location selected, but I’m confused how to get data that just disappear like the case above. I tried to make if condition, if value of location is not increased, it shows an alert javascript, but it’s not showing an alert

Here’s the code

 $('.select2').on('change', function() {
    var test = $("#location").val(); 
    console.log(test) // shows value in array like ["location 1","location 2",..]
    var a = test.length
    if(a != a+1)
    {
        alert("you already input that location"); //shows this alert if length array is not increased
    }
 })

Do you know how to fix this ? thank you