<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>重複カウント&割合表示</title>
</head>
<body>
<h2>テキスト入力</h2>
<textarea id="inputText" rows="10" cols="30"></textarea><br>
<button onclick="processText()">実行</button>
<h2>結果</h2>
<div id="result"></div>
<script>
function processText() {
const text = document.getElementById('inputText').value;
const lines = text.split('\n').map(line => line.trim()).filter(line => line !== '');
const countMap = new Map();
lines.forEach(line => {
countMap.set(line, (countMap.get(line) || 0) + 1);
});
const total = lines.length;
// 件数順にソート
const sorted = Array.from(countMap.entries()).sort((a, b) => b[1] - a[1]);
// 結果表示
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
sorted.forEach(([text, count]) => {
const percent = Math.round((count / total) * 100);
resultDiv.innerHTML += `<div>${text} ${count}件(${percent}%)</div>`;
});
}
</script>
</body>
</html>
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme