PHP Distinct
function distinct(array $a): int
{
return count(array_flip($a));
}
This counts unique values by tracking what has already been seen.
Python Distinct
def distinct(a: list[int]) -> int:
return len(set(a))
This counts unique values by tracking what has already been seen.
Rust Distinct
use std::collections::HashSet;
fn distinct(a: &[i64]) -> i64 {
let set: HashSet<_> = a.iter().collect();
set.len() as i64
}
This counts unique values by tracking what has already been seen.
TypeScript Distinct
function distinct(a: number[]): number {
return new Set(a).size;
}
This counts unique values by tracking what has already been seen.
Bash Dominator
dominator() {
local -n _a="$1"
local _size=0 _value=0 _count=0 _index=0
local _k _v
for _k in "${!_a[@]}"; do
_v=${_a[$_k]}
if (( _size == 0 )); then
((_size++))
_value=$_v
_index=$_k
elif (( _value != _v )); then
((_size--))
else
((_size++))
fi
done
local _candidate
if (( _size > 0 )); then _candidate=$_value; else _candidate=-1; fi
_count=0
for _v in "${_a[@]}"; do
if (( _v == _candidate )); then ((_count++)); fi
done
if (( _count <= ${#_a[@]} / 2 )); then
_index=-1
fi
echo "$_index"
}
This finds a value that appears in more than half of the array, then returns one valid index for it.
C++ Dominator
#include <cstddef>
#include <vector>
int dominator(const std::vector<int>& a)
{
int size = 0;
int value = 0;
int index = 0;
for (std::size_t k = 0; k < a.size(); ++k) {
int v = a[k];
if (size == 0) {
++size;
value = v;
index = static_cast<int>(k);
} else if (value != v) {
--size;
} else {
++size;
}
}
int candidate = size > 0 ? value : -1;
int count = 0;
for (int v : a) {
if (v == candidate) {
++count;
}
}
if (count <= static_cast<int>(a.size()) / 2) {
index = -1;
}
return index;
}
This finds a value that appears in more than half of the array, then returns one valid index for it.
C# Dominator
static int Dominator(int[] a)
{
var size = 0;
var value = 0;
var index = 0;
for (int k = 0; k < a.Length; k++)
{
var v = a[k];
if (size == 0)
{
size++;
value = v;
index = k;
}
else if (value != v)
{
size--;
}
else
{
size++;
}
}
var candidate = size > 0 ? value : -1;
var count = 0;
foreach (var v in a)
{
if (v == candidate)
{
count++;
}
}
if (count <= a.Length / 2.0)
{
index = -1;
}
return index;
}
This finds a value that appears in more than half of the array, then returns one valid index for it.
Elixir Dominator
defmodule Dominator do
def dominator(a) do
{size, value, index} =
a
|> Enum.with_index()
|> Enum.reduce({0, nil, nil}, fn {v, k}, {size, value, index} ->
cond do
size == 0 -> {1, v, k}
value != v -> {size - 1, value, index}
true -> {size + 1, value, index}
end
end)
candidate = if size > 0, do: value, else: -1
count = Enum.count(a, &(&1 == candidate))
if count > div(length(a), 2), do: index, else: -1
end
end
This finds a value that appears in more than half of the array, then returns one valid index for it.
Erlang Dominator
-module(dominator).
-export([dominator/1]).
dominator(A) ->
Indexed = lists:zip(lists:seq(0, length(A) - 1), A),
{FinalSize, FinalValue, FinalIndex} = lists:foldl(fun({K, V}, {Size, Value, Index}) ->
case Size of
0 -> {1, V, K};
_ ->
case Value =:= V of
true -> {Size + 1, Value, Index};
false -> {Size - 1, Value, Index}
end
end
end, {0, undefined, 0}, Indexed),
Candidate = case FinalSize > 0 of
true -> FinalValue;
false -> -1
end,
Count = length([X || X <- A, X =:= Candidate]),
case Count > length(A) / 2 of
true -> FinalIndex;
false -> -1
end.
This finds a value that appears in more than half of the array, then returns one valid index for it.
Go Dominator
func dominator(a []int) int {
size, value, index := 0, 0, 0
for k, v := range a {
switch {
case size == 0:
size++
value = v
index = k
case value != v:
size--
default:
size++
}
}
candidate := -1
if size > 0 {
candidate = value
}
count := 0
for _, v := range a {
if v == candidate {
count++
}
}
if count <= len(a)/2 {
index = -1
}
return index
}
This finds a value that appears in more than half of the array, then returns one valid index for it.