SoFunction
Updated on 2025-04-10

JS implements a method to find duplicate lines for sorted strings

This article describes how JS implements the method of finding duplicate lines as sorted strings. Share it for your reference, as follows:

To achieve such a requirement, in an Editplus document, there are many 10-digit numbers in rows, and these numbers are already sorted.

for example:

1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573

Is there any way to easily find the same number in the first 7 digits in the two rows?

For example, the above numbers can be found

1234567890
1234567891
1234567892

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http:///TR/xhtml1/DTD/">
<html>
<head>
  <title></title>
  <style type="text/css">
    div{ float:left; }
    #divCenter{ padding-top:100px;margin:0 50px; }
    .txt{width:200px;height:200px;}
    #txtOldData{background-color:#A6CAF0;}
    #txtAnswer{background-color:#EBA9A6;}
  </style>
  <script type="text/javascript">
    function test() {
      var arr = ("txtOldData").(/ +/g, '').split("\n");
      var tempStr = arr[0].substring(0, 7);
      var compareLen = 7, equalNum = 0;
      var answer = "";
      for (var i = 1; i < ; i++) {
        if (arr[i].substring(0, 7) == tempStr) {
          if (equalNum == 0)
            answer += arr[i - 1] + "\n";
          answer += arr[i] + "\n";
          equalNum++;
        } else {
          tempStr = arr[i].substring(0, 7);
          equalNum = 0;
        }
      }
      ("txtAnswer").value = (answer);
    }
  </script>
</head>
<body>
  <div>
    Please enter a value:<br />
    <textarea  class="txt">
1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573
    </textarea>
  </div>
  <div style="padding-top:90px;padding" >
    <input type="button" value="test==>" onclick="test()" />
  </div>
  <div>
    result:<br />
    <textarea  class="txt"></textarea>
  </div>
</body>
</html>

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.