0

I am trying to achieve the following result. I have a textarea, I want to transform all the text (by line) that will be input inside this textarea in an array. Once the text has been input, I want to check if there are duplicates in the entries.

To do so, I: 1) create an array (named myarrayfromtextarea, below) from the textarea 2) I create an array (named checkArray below) of the duplicates value 3) I loop through all the values in my first array and I check for matches in the second array.

Theoretically, it works well but I tried to input the following values in the textarea:

  • dwdw
  • dwdwdwdwdwdwdwdw
  • dwdwdwdw
  • dwdwdwdw
  • dwdwdwdwdwdwdwdwdwdw

I expected to find only one duplicate value (row 3 and 4) but incredibly, when I run --> checkArray.indexOf(array[i]) also the row 1 shows a match.

Can you help me?

var myarrayfromtextarea = $('#My_textarea').val().split('\n'); // create array from textarea
console.log("array from textarea: " + myarrayfromtextarea)


var myArr = myarrayfromtextarea; /// here I start to find duplicates
var obj = {};
var checkArray = "["
myArr.forEach(function(item) {
    if (typeof obj[item] == 'number') {
        checkArray = checkArray + item + ",";
        obj[item]++;

    } else {
        obj[item] = 1;
    }
});

checkarraylenght = (checkArray.length - 1)
checkArray = checkArray.substring(0, checkarraylenght)


checkArray = checkArray + "]" + '';
console.log("checkarray = " + checkArray)

myarrayfromtextarea = myarrayfromtextarea + '';
var array = myarrayfromtextarea.split(',');

var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {

    if (checkArray.indexOf(array[i]) == -1) {
        console.log("Not a duplicate: " + array[i]);
    } else {

        console.log("Duplicate value: " + array[i]);
    }
    //Do something
}
3
  • 1
    checkArray is a string, and therefore you are using String#indexOf which returns partial matches, as opposed to Array#indexOf which only returns exact matches. There really is no point in the checkArray variable, though. You already create an object with the values - just iterate over the keys and see if the obj[key] > 1. If so, you know there are duplicates. Commented Aug 4, 2017 at 15:21
  • I understand your explanation about the string / array but I am a bit lost on how to modify my code to make it works.. Commented Aug 4, 2017 at 15:35
  • See Taplar's solution. Use actual arrays, rather than string interpretations of arrays. Commented Aug 4, 2017 at 15:52

3 Answers 3

3

//maybe something like
var inputLines = $('textarea').val().split('\n');
var lineValues = {};

inputLines.forEach(function(line, index){
  if (!lineValues[line]) lineValues[line] = [];
  
  lineValues[line].push(index + 1);
});

Object.keys(lineValues).forEach(function(line){
  if (lineValues[line].length > 1) {
    lineValues[line].forEach(function(value){
      console.log(['Duplicate Value "', line, '":line:', value ].join(''));
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>
dwdw
dwdwdwdwdwdwdwdw
dwdwdwdw
dwdwdwdw
dwdwdwdwdwdwdwdwdwdw
</textarea>
<div><button>TEST</button></div>

Sign up to request clarification or add additional context in comments.

Comments

0

Quick and dirty using jQuery:

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
    if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});

2 Comments

O(n^2) time complexity is less than optimal
Also, the point of this question is not to get a list of unique values - it's to determine which values are duplicates.
0

This code returns all duplicated index!

ref: find duplication in string, textarea's lines

var a= [], j= -1, obj = $('textarea').val().split('\n');
for(var i in obj){
  j= -1;
  while((j= obj.indexOf(obj[i],j+1)) < 1) 
    a.push(i);
}
console.log(a)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea rows="4" cols="50">
ok
ok.
nok
ok
</textarea>

2 Comments

Not exactly best practice to implicitly use the Array object with indexes as keys and use inline assignment inside of an expression. It is safe here, but as a general practice of writing JavaScript, this approach is frowned upon.
@mhodges, I do not know where it may have a problem, may you explain more?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.