Distinct Array Values in Python and C++ Codility Solutions Lesson 6

preview_player
Показать описание
Hi, this video describes the solution to codility lesson 6 about counting Distinct Array Values, the solution is then written in Python and in C++ with code description. A Good exercise to prepare for coding interviews and remote developer position, many interviewers use codility in their tests. Good luck and happy coding. python algorithms interview.

🍓 If you want to follow structured courses with more details and practice exercises check my "About" page for Discount Coupons on my Udemy courses covering: Python basics, Object Oriented Programming and Data Analysis with NumPy and Pandas, ... more courses are on the way drop me a message if you have a particular interesting topic! Good luck!

#python #cplusplusprogramming #codinginterview
Рекомендации по теме
Комментарии
Автор

#include <set>
int solution(vector<int> &A) {
set<int> s(A.begin(), A.end());
return s.size();
}

babakrahi
Автор

A set solution in C++ can be achieved in a similar way

#include <unordered_set>
int solution(vector<int> &A)
{
unordered_set<int> distinct_values(A.begin(), A.end());
return distinct_values.size();
}

tomzopo
Автор

With C# I kinda did something that might not be RAM efficient but I believe just a bit more CPU efficient.
var arrNeg = new int[1_000_000];
var arrPos = new int[1_000_000];

for (int i = 0; i < A.Length; i++)
{
if (A[i] < 0)
arrNeg[Math.Abs(A[i])] = 1;
else
arrPos[Math.Abs(A[i])] = 1;
}

return arrNeg.Sum() + arrPos.Sum();

I Used indexes for the numbers in the array and assigned them 1 then sum'em up.
This did a 100% in Codility.

SEA_
Автор

Python: No need to sort. This calls for a dictionary. Using a Set is great too, but the getting a set is exactly what this is asking for, so using set is almost like someo wrote the code for you.
With a dictionary (comprehension) you can do this:

def
.. x = { num: 0 for num in A }
.. return lex(x)

financialquant
Автор

Do we need loops here? I dont think so.

def solution(A):
if len(A) < 1:
return "This is an empty array"
unique_integers = len(set(A))
return unique_integers

afeeist
join shbcf.ru