TypeScript Cyclic Rotation
function cyclicRotation(a: number[], k: number): number[] {
const result = [...a];
if (result.length > 0) {
for (let i = 0; i < k; i++) {
const popped = result.pop() as number;
result.unshift(popped);
}
}
return result;
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Bash Distinct
distinct() {
local -n _a="$1"
local -A _seen
local _v
for _v in "${_a[@]}"; do _seen[$_v]=1; done
echo "${#_seen[@]}"
}
This counts unique values by tracking what has already been seen.
C++ Distinct
#include <unordered_set>
#include <vector>
int distinct(const std::vector<int>& a)
{
std::unordered_set<int> unique(a.begin(), a.end());
return static_cast<int>(unique.size());
}
This counts unique values by tracking what has already been seen.
C# Distinct
static int Distinct(int[] a)
{
return new HashSet<int>(a).Count;
}
This counts unique values by tracking what has already been seen.
Elixir Distinct
defmodule Distinct do
def distinct(a), do: a |> Enum.uniq() |> length()
end
This counts unique values by tracking what has already been seen.
Erlang Distinct
-module(distinct).
-export([distinct/1]).
distinct(A) ->
length(lists:usort(A)).
This counts unique values by tracking what has already been seen.
Go Distinct
func distinct(a []int) int {
seen := make(map[int]struct{}, len(a))
for _, v := range a {
seen[v] = struct{}{}
}
return len(seen)
}
This counts unique values by tracking what has already been seen.
Haskell Distinct
import qualified Data.Set as Set
distinct :: [Int] -> Int
distinct a = Set.size (Set.fromList a)
This counts unique values by tracking what has already been seen.
Java Distinct
import java.util.Arrays;
public class Solution {
public static int distinct(int[] a) {
return (int) Arrays.stream(a).distinct().count();
}
}
This counts unique values by tracking what has already been seen.
Lisp Distinct
(defun distinct (a)
(length (remove-duplicates a :test #'equal)))
This counts unique values by tracking what has already been seen.