LouvainUtils { static int normalize(int[] communities) { final IntIntMap map = new IntIntScatterMap(communities.length); int c = 0; for (int i = 0; i < communities.length; i++) { int mapped, community = communities[i]; if ((mapped = map.getOrDefault(community, -1)) != -1) { communities[i] = mapped; } else { map.put(community, c); communities[i] = c++; } } return c; }    }
@Test public void differentNumbers() { int[] communities = {10, 3, 4, 7, 6, 7, 10}; assertEquals(5, LouvainUtils.normalize(communities)); assertArrayEquals(new int[]{0, 1, 2, 3, 4, 3, 0}, communities); }
@Test public void allTheSame() { int[] communities = {10, 10, 10, 10}; assertEquals(1, LouvainUtils.normalize(communities)); assertArrayEquals(new int[]{0, 0, 0, 0}, communities); }
@Test public void allDifferent() { int[] communities = {1, 2, 3, 4, 7, 5}; assertEquals(6, LouvainUtils.normalize(communities)); assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, communities); }