본문 바로가기

공부하자/Codility

[Codility] Lesson6. Distinct (C#)

문제: app.codility.com/programmers/lessons/6-sorting/distinct/

 

Distinct coding task - Learn to Code - Codility

Compute number of distinct values in an array.

app.codility.com

 

 

 

            if (A.Length == 0)
                return 0;

            List<int> ListA = new List<int>(A);
            ListA.Sort();
            int result = 1;
            int index = ListA[0];
            for (int i = 0; i < ListA.Count; i++)
            {
                if (ListA[i] != index)
                {
                    result = result + 1;
                    index = ListA[i];
                }
            }
            return result;