Skip to content

Instantly share code, notes, and snippets.

@lenka21
Last active August 29, 2015 14:20
Show Gist options
  • Save lenka21/4dcc5211384503c42b67 to your computer and use it in GitHub Desktop.
Save lenka21/4dcc5211384503c42b67 to your computer and use it in GitHub Desktop.
dominanta
<!DOCTYPE html>
<html>
<head lang="pl">
<meta charset="UTF-8">
<script src="main.js"></script>
<title>most often</title>
</head>
<body>
<h2>Podaj elementy tablicy, oddzielone przecinkami</h2><input id="data">
<button onclick="often()">Run</button>
<h2 id="view"></h2>
<h2 id="view2"></h2>
</body>
</html>
function often() {
var data = document.getElementById("data").value;
var array = data.split(",");
console.log(array);
var max_n = 0;
var max_l = 0;
var grups = {};
for (var i = 0; i < array.length; i++) {
if (grups[array[i]]) {
grups[array[i]] += 1;
} else {
grups[array[i]] = 1;
}
}
for (var n in grups) {
console.log(n);
console.log(grups[n]);
if (grups[n] > max_l) {
max_l = grups[n]
max_n = n;
} else if (grups[n] == max_l) {
max_n = max_n +" " +n;
}
}
document.getElementById("view").innerHTML = (max_l + " razy wystepuja elementy: " );
document.getElementById("view2").innerHTML = (max_n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment