C - Uniqueness
Source: atcoder
Difficulty: 300

Problem Statement
There are
N
people, labeled
1
to
N
. Person
i
has an integer
A_i
.
Among the people who satisfy the condition "None of the other
N-1
people has the same integer as themselves," find the one with the greatest integer, and print that person's label.
If no person satisfies the condition, report that fact instead.
Constraints
1 \leq N \leq 3\times 10^5
1 \leq A_i \leq 10^9
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1
A_2
\ldots
A_N
Output
If no person satisfies the condition "None of the other
N-1
people has the same integer as themselves," print
-1
.
Otherwise, among those who satisfy it, print the label of the person whose integer is the largest.
Sample Input 1
9
2 9 9 7 9 2 4 5 8
Sample Output 1
9
Those who satisfy the condition are the persons labeled
4
,
7
,
8
, and
9
.
Their integers are
7
,
4
,
5
, and
8
, respectively, and the person with the largest integer is the person labeled
9
.
Thus, the answer is
9
.
Sample Input 2
4
1000000000 1000000000 998244353 998244353
Sample Output 2
-1
If no person satisfies the condition, print
-1
.

C++ solution:
#include <bits/stdc++.h>

using namespace std;
const int N = 300010;
int n, a[N];

int main() {
    scanf("%d", &n);
    unordered_map<int, int> counter;
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        counter[a[i]]++;
    }
    int ans = -1, mx = 0;
    for(int i = 1; i <= n; i++) {
        if(mx < a[i] && counter[a[i]] == 1) {
            ans = i;
            mx = a[i];
        }
    }
    printf("%d\n", ans);
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func outSlice[T any](s []T) {
	if len(s) == 0 {
		return
	}
	for i := 0; i < len(s)-1; i++ {
		fmt.Fprint(wr, s[i], " ")
	}
	fmt.Fprintln(wr, s[len(s)-1])
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// min for n entry
func nmin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

func chmin(a *int, b int) bool {
	if *a < b {
		return false
	}
	*a = b
	return true
}

func chmax(a *int, b int) bool {
	if *a > b {
		return false
	}
	*a = b
	return true
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, math.MaxInt32)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N := getI()
	a := getInts(N)

	m := make(map[int]int)
	for i := 0; i < N; i++ {
		m[a[i]]++
	}

	maxm := -1
	for e := range m {
		if m[e] == 1 {
			maxm = max(maxm, e)
		}
	}

	for i := 0; i < N; i++ {
		if a[i] == maxm {
			out(i + 1)
			return
		}
	}
	out(-1)
}

Java solution:
import java.io.*;
import java.util.*;
import java.util.function.*;

public class Main {
  private static int solve(int n, int[] as) {
    int[] bs = new int[n + 2];
    for (int i = 0; i < n; i++) {
      bs[i] = as[i];
    }
    bs[n] = -1;
    bs[n + 1] = 2_000_000_000;
    Arrays.sort(bs);
    for (int i = n; i >= 1; i--) {
      if (bs[i + 1] > bs[i] && bs[i] > bs[i - 1]) {
        for (int j = 0; j < n; j++) {
          if (as[j] == bs[i]) {
            return j + 1;
          }
        }
      }
    }
    return -1;
  }

  private static void execute(ContestReader reader, ContestWriter out) {
    int n = reader.nextInt();
    int[] as = reader.nextInt(n);
    out.println(solve(n, as));
  }

  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    ContestWriter out = new ContestWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}


class ContestWriter extends PrintWriter {
  ContestWriter(PrintStream printStream) {
    super(printStream);
  }

  public void printYN(boolean b) {
    println(b ? "Yes" : "No");
  }

  public void printYNList(List<Boolean> bList) {
    for (boolean b : bList) {
      printYN(b);
    }
  }

  public void printList(List<? extends Object> list) {
    for (Object object : list) {
      println(object);
    }
  }

  public void printListOneLine(List<? extends Object> list) {
    List<String> stringList = new ArrayList<>();
    for (Object object : list) {
      stringList.add(object.toString());
    }
    println(String.join(" ", stringList));
  }
}


class ContestReader {
  private static final int BUFFER_SIZE = 1024;

  private final InputStream stream;
  private final byte[] buffer;
  private int pointer;
  private int bufferLength;

  ContestReader(InputStream stream) {
    this.stream = stream;
    this.buffer = new byte[BUFFER_SIZE];
    this.pointer = 0;
    this.bufferLength = 0;
  }

  private boolean hasNextByte() {
    if (pointer < bufferLength) {
      return true;
    }

    pointer = 0;
    try {
      bufferLength = stream.read(buffer);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return bufferLength > 0;
  }

  private int readByte() {
    if (hasNextByte()) {
      return buffer[pointer++];
    } else {
      return -1;
    }
  }

  private static boolean isPrintableChar(int c) {
    return 33 <= c && c <= 126;
  }

  public boolean hasNext() {
    while (hasNextByte() && !isPrintableChar(buffer[pointer])) {
      pointer++;
    }
    return hasNextByte();
  }

  public String next() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
    StringBuilder sb = new StringBuilder();
    while (true) {
      int b = readByte();
      if (!isPrintableChar(b)) {
        break;
      }
      sb.appendCodePoint(b);
    }
    return sb.toString();
  }

  public String nextLine() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
    StringBuilder sb = new StringBuilder();
    while (true) {
      int b = readByte();
      if (!isPrintableChar(b) && b != 0x20) {
        break;
      }
      sb.appendCodePoint(b);
    }
    return sb.toString();
  }

  public char nextChar() {
    return next().charAt(0);
  }

  public int nextInt() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }

    int n = 0;
    boolean minus = false;

    {
      int b = readByte();
      if (b == '-') {
        minus = true;
      } else if ('0' <= b && b <= '9') {
        n = b - '0';
      } else {
        throw new NumberFormatException();
      }
    }

    while (true) {
      int b = readByte();
      if ('0' <= b && b <= '9') {
        n *= 10;
        n += b - '0';
      } else if (b == -1 || !isPrintableChar(b)) {
        return minus ? -n : n;
      } else {
        throw new NumberFormatException();
      }
    }
  }

  public long nextLong() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }

    long n = 0;
    boolean minus = false;

    {
      int b = readByte();
      if (b == '-') {
        minus = true;
      } else if ('0' <= b && b <= '9') {
        n = b - '0';
      } else {
        throw new NumberFormatException();
      }
    }

    while (true) {
      int b = readByte();
      if ('0' <= b && b <= '9') {
        n *= 10;
        n += b - '0';
      } else if (b == -1 || !isPrintableChar(b)) {
        return minus ? -n : n;
      } else {
        throw new NumberFormatException();
      }
    }
  }

  public double nextDouble() {
    return Double.parseDouble(next());
  }

  public String[] next(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }

  public String[] nextLine(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLine();
    }
    return array;
  }

  public char[] nextChar(int n) {
    char[] array = new char[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextChar();
    }
    return array;
  }

  public int[] nextInt(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }

  public long[] nextLong(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }

  public double[] nextDouble(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }

  public char[] nextCharArray() {
    return next().toCharArray();
  }

  public String[][] next(int n, int m) {
    String[][] matrix = new String[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = next();
      }
    }
    return matrix;
  }

  public int[][] nextInt(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }

  public char[][] nextChar(int n, int m) {
    char[][] matrix = new char[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextChar();
      }
    }
    return matrix;
  }

  public long[][] nextLong(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }

  public double[][] nextDouble(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }

  public char[][] nextCharArray(int n) {
    char[][] matrix = new char[n][];
    for (int i = 0; i < n; i++) {
      matrix[i] = next().toCharArray();
    }
    return matrix;
  }
}


class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>> {
  T first;
  U second;

  Pair(T first, U second) {
    this.first = first;
    this.second = second;
  }

  public int compareTo(Pair<T, U> p) {
    int tResult = first.compareTo(p.first);
    if (tResult != 0) {
      return tResult;
    }
    return second.compareTo(p.second);
  }
}


class MyAssert {
  public static void myAssert(boolean flag, String message) {
    if (!flag) {
      throw new RuntimeException(message);
    }
  }

  public static void myAssert(boolean flag) {
    myAssert(flag, "");
  }
}

JavaScript solution:
function Main(input) {
  input = input.split("\n").map((line) => line.trim());
  const N = Number(input[0]);
  const A = input[1].split(" ").map(Number);
  const sortedA = A.slice().sort((a, b) => b - a);
  sortedA.unshift(Infinity);
  sortedA.push(Infinity);
  let unique = -1;
  for (let i = 1; i < sortedA.length - 1; i++) {
    if (sortedA[i] !== sortedA[i + 1] && sortedA[i] !== sortedA[i - 1]) {
      unique = sortedA[i];
      break;
    }
  }

  let ans = -1;
  for (let i = 0; i < N; i++) {
    if (A[i] == unique) ans = i + 1;
  }
  console.log(ans === -1 ? -1 : ans);
}

Main(require("fs").readFileSync(0, "utf8"));

Python solution:
from collections import Counter
N = int(input())
input_li = list(map(int, input().split()))
nums = Counter(input_li)

li = []
for key, values in nums.items():
    if values == 1:
        li.append(key)
    else:
        li.append(-1)

if all(i == -1 for i in li):
    print(-1)
    exit(0)

print(input_li.index(max(li)) + 1)

Ruby solution:
N,*A = $<.read.split.map(&:to_i)
T = A.tally
X = T.keys.select{|a| T[a]==1 }.max
puts(X ? A.index(X)+1 : -1)