Elixir Odd Occurrences In Array
defmodule OddOccurrencesInArray do
def odd_occurrences_in_array(a) do
a
|> Enum.reduce(%{}, fn v, count ->
if Map.has_key?(count, v), do: Map.delete(count, v), else: Map.put(count, v, 1)
end)
|> Map.keys()
|> List.first()
end
end
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Erlang Odd Occurrences In Array
-module(odd_occurrences_in_array).
-export([odd_occurrences_in_array/1]).
odd_occurrences_in_array(A) ->
lists:foldl(fun(X, Acc) -> Acc bxor X end, 0, A).
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Go Odd Occurrences In Array
// oddOccurrencesInArray assumes exactly one value occurs an odd number of
// times (per the problem's constraints) and recovers it via XOR.
func oddOccurrencesInArray(a []int) int {
result := 0
for _, v := range a {
result ^= v
}
return result
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Haskell Odd Occurrences In Array
import Data.Bits (xor)
oddOccurrencesInArray :: [Int] -> Maybe Int
oddOccurrencesInArray [] = Nothing
oddOccurrencesInArray a = Just (foldl1 xor a)
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Java Odd Occurrences In Array
import java.util.LinkedHashMap;
import java.util.Map;
public class Solution {
public static Integer oddOccurrencesInArray(int[] a) {
Map<Integer, Integer> count = new LinkedHashMap<>();
for (int value : a) {
if (!count.containsKey(value)) {
count.put(value, 1);
} else {
count.remove(value);
}
}
for (Integer key : count.keySet()) {
return key;
}
return null;
}
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Lisp Odd Occurrences In Array
(defun odd-occurrences-in-array (a)
(let ((counts (make-hash-table :test 'eql)))
(dolist (v a)
(incf (gethash v counts 0)))
(loop for v being the hash-keys of counts using (hash-value c)
when (oddp c)
return v)))
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
PHP Odd Occurrences In Array
function oddOccurrencesInArray(array $a): ?int
{
$count = [];
foreach ($a as $value) {
if ( ! isset($count[$value])) {
$count[$value] = 1;
} else {
unset($count[$value]);
}
}
return key($count);
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Python Odd Occurrences In Array
def odd_occurrences_in_array(a: list[int]) -> int | None:
count: dict[int, int] = {}
for value in a:
if value not in count:
count[value] = 1
else:
del count[value]
return next(iter(count), None)
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Rust Odd Occurrences In Array
use std::collections::HashMap;
fn odd_occurrences_in_array(a: &[i64]) -> Option<i64> {
let mut count: HashMap<i64, bool> = HashMap::new();
for &value in a {
if count.contains_key(&value) {
count.remove(&value);
} else {
count.insert(value, true);
}
}
count.keys().next().copied()
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
TypeScript Odd Occurrences In Array
function oddOccurrencesInArray(a: number[]): number | null {
const count = new Map<number, number>();
for (const value of a) {
if (!count.has(value)) {
count.set(value, 1);
} else {
count.delete(value);
}
}
const first = count.keys().next();
return first.done ? null : first.value;
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.