Java Utililty Methods Array Unique

List of utility methods to do Array Unique

Description

The list of methods to do Array Unique are organized into topic(s).

Method

intcountUnique(int[][] array)
count Unique
if (array.length == 0) {
    return 0;
BitSet values = new BitSet(ARRAY_MAX);
int count = 1;
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length - 1; j++) {
        if (!values.get(array[i][j])) {
...
int[]getUniqueIDsArray(int[] idArray, String ids, String seperator)
get Unique I Ds Array
Set<Integer> idSet = new HashSet<Integer>();
if (idArray != null) {
    for (int id : idArray) {
        idSet.add(id);
int[] tmpIdArray = transIDsToArray(ids, seperator);
if (tmpIdArray != null) {
...
String[]getUniqueItems(final String[] allItems, final String newItem, final int maxItems)
Remove duplicate items from a list and add a new item.
if (newItem == null) {
    return allItems;
final ArrayList<String> newItems = new ArrayList<String>();
newItems.add(newItem);
for (final String oldItem : allItems) {
    if (newItem.equals(oldItem) == false) {
        newItems.add(oldItem);
...
String[]getUniqueWords(String[] input)
Find all unique words in an array of words
if (input == null) {
    return new String[0];
} else {
    Set result = new TreeSet();
    for (int i = 0; i < input.length; i++) {
        result.add(input[i]);
    return (String[]) result.toArray(new String[result.size()]);
...
int[]invertRelabeling(int[] relabeling, int[] uniqueVars, int maxVarNum)
invert Relabeling
int[] inverseRelabeling = new int[maxVarNum + 1];
Arrays.fill(inverseRelabeling, -1);
for (int i = 0; i < relabeling.length; i++) {
    if (relabeling[i] != -1) {
        inverseRelabeling[relabeling[i]] = uniqueVars[i];
return inverseRelabeling;
...
double[]unique(double[] in)
Same behaviour as mathlab unique.
Arrays.sort(in);
int n = in.length;
double[] temp = new double[n];
temp[0] = in[0];
int count = 1;
for (int i = 1; i < n; i++) {
    if (Double.compare(in[i], in[i - 1]) != 0) {
        temp[count++] = in[i];
...
int[]unique(int[] a, int aLen, int[] b, int bLen)
Returns array which is the union of two arrays (array of elements contained in any of provided arrays).
assert a != null;
assert b != null;
assert isIncreasingArray(a, aLen);
assert isIncreasingArray(b, bLen);
int[] res = new int[aLen + bLen];
int resLen = 0;
int i = 0;
int j = 0;
...
int[]unique(int[] array)
Returns the same values of the input array but with no repetitions.
Set<Integer> uniqueSet = new LinkedHashSet<Integer>();
for (int i = 0; i < array.length; i++)
    uniqueSet.add(array[i]);
return toArray(uniqueSet);
Object[]unique(Object[] elements)
Return a set (array of unique objects).
Hashtable h = new Hashtable();
Object o = new Object();
for (int i = 0; i < elements.length; i++) {
    h.put(elements[i], o);
Object[] el2 = new Object[h.size()];
Enumeration e = h.keys();
int i = 0;
...
int[]uniqueInts(int[] ints)
returns the unique values from the given int array in sort order.
Arrays.sort(ints);
int duplicateCount = 0;
for (int i = 1; i < ints.length; i++) {
    if (ints[i] == ints[i - 1]) {
        duplicateCount++;
if (duplicateCount == 0) {
...