We can use the Elbow method to determine the optimal number of cluster for k-Means Clustering, as well as, the optimal k value for k-Nearest Neighbour. Although both starts with "K", k-means clustering is not the same as k-Nearest Neighbour. The k-Nearest Neighbour or KNN algorithm is doing exactly as what it's name suggests, using k number of nearest neighbours to classify the sample point.
The sample code for KNN is as follows: