Home > Web Front-end > JS Tutorial > JS implements a method to find duplicate rows for sorted strings

JS implements a method to find duplicate rows for sorted strings

PHPz
Release: 2018-10-13 17:59:24
Original
1561 people have browsed it

This article mainly introduces the method of JS implementation to find duplicate lines for sorted strings, involving JavaScript string operation related skills, which has certain reference value. Friends in need can refer to it

To realize such a requirement, in an Editplus document, there are many rows of 10-digit numbers, and these numbers have been sorted.

For example:

1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573

Is there any way to make it easier? Find two lines with at least the first 7 digits that are the same? [ Recommended related tutorials】

1.

JavaScript video tutorial


2.
JavaScript online manual

3.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title></title>
  <style type="text/css">
    p{ float:left; }
    #pCenter{ 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 = document.getElementById("txtOldData").value.replace(/ +/g, &#39;&#39;).split("\n");
      var tempStr = arr[0].substring(0, 7);
      var compareLen = 7, equalNum = 0;
      var answer = "";
      for (var i = 1; i < arr.length; 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;
        }
      }
      document.getElementById("txtAnswer").value = (answer);
    }
  </script>
</head>
<body>
  <p>
    请输入数值:<br />
    <textarea id="txtOldData" class="txt">
1234567890
1234567891
1234567892
1234534124
1234614124
4321412414
5636373573
    </textarea>
  </p>
  <p style="padding-top:90px;padding" >
    <input type="button" value="测试==>" onclick="test()" />
  </p>
  <p>
    结果:<br />
    <textarea id="txtAnswer" class="txt"></textarea>
  </p>
</body>
</html>
Copy after login
bootstrap tutorial

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template