Skip to content

Instantly share code, notes, and snippets.

@noisy
Forked from lenka21/index.html
Last active August 29, 2015 14:20
Show Gist options
  • Save noisy/11d19cdeb3ac399dffe4 to your computer and use it in GitHub Desktop.
Save noisy/11d19cdeb3ac399dffe4 to your computer and use it in GitHub Desktop.
<!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 dominant = 0;
var dominant_counter = 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] > dominant_counter) {
dominant_counter = grups[n]
dominant = n;
} else if (grups[n] == dominant_counter) {
dominant = dominant +" " +n;
}
}
document.getElementById("view").innerHTML = (dominant_counter + " razy wystepuja elementy: " );
document.getElementById("view2").innerHTML = (dominant);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment