How do I create array of unique values from another array?

Category: System, viewed: 193 time(s).

The following code snippet show you how to create an array that contains a unique value of another array. In this snippet we use an array of numbers as an example.

The isUnique() method will check if the number exists in the array or not. While the toUniqueArray() method will check and filter the given array and create a new array with only unique number.

using System;

namespace Kodecsharp.Example.System
{
    class UniqueArray
    {
        /// <summary>
        /// Returns true if the array doesn't contains the number.
        /// </summary>
        public static bool isUnique(int[] array, int number)
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == number)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Convert the given array into another array with unique 
        /// values.
        /// </summary>
        public static int[] toUniqueArray(int[] array)
        {
            int[] temp = new int[array.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = -1;
            }

            int counter = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (isUnique(temp, array[i])) 
                {
                    temp[counter++] = array[i];
                }
            }

            int[] uniqueArray = new int[counter];
            Array.Copy(temp, 0, uniqueArray, 0, uniqueArray.Length);
            return uniqueArray;
        }

        /// <summary>
        /// Print the content of given array
        /// </summary>
        public static void printArray(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine("");
        }

        public static void Main(string[] args)
        {
            int[] array = { 1, 1, 2, 3, 4, 1, 4, 7, 9, 7 };
            printArray(array);
            printArray(toUniqueArray(array));
        }
    }
}
Powered by Disqus