Here you can find the source of invertMap(int[] map)
public static int[] invertMap(int[] map)
//package com.java2s; /**// www .j av a 2 s. co m * * Copyright 2017 Florian Erhard * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.util.Arrays; public class Main { public static int[] invertMap(int[] map) { int[] re = new int[max(map) + 1]; Arrays.fill(re, -1); for (int i = 0; i < map.length; i++) re[map[i]] = i; return re; } public static int max(int[] a) { int re = Integer.MIN_VALUE; for (int i : a) re = Math.max(re, i); return re; } public static int max(int[] a, int start, int end) { int re = Integer.MIN_VALUE; for (int i = start; i < end; i++) re = Math.max(re, a[i]); return re; } public static long max(long[] a, int start, int end) { long re = Long.MIN_VALUE; for (int i = start; i < end; i++) re = Math.max(re, a[i]); return re; } public static double max(double[] a, int start, int end) { double re = Double.NEGATIVE_INFINITY; for (int i = start; i < end; i++) re = Math.max(re, a[i]); return re; } public static int max(int[][] m) { int re = Integer.MIN_VALUE; for (int[] a : m) for (int i : a) re = Math.max(re, i); return re; } public static float max(float[] a) { float re = a[0]; for (float i : a) re = Math.max(re, i); return re; } /** * Computes the max of the given array. * @param array the array * @return the max */ public static double max(double[] array) { if (array.length == 0) return Double.NEGATIVE_INFINITY; double re = array[0]; for (int i = 1; i < array.length; i++) re = Math.max(re, array[i]); return re; } }