Created
July 6, 2016 15:44
-
-
Save tiagopereira17/a29ee4965a391bd55b8a1fa44327b1ae to your computer and use it in GitHub Desktop.
Compute number of distinct values in an array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Distinct { | |
public int distinctElements(int[] A) { | |
if(A == null || A.length == 0) { | |
return 0; | |
} | |
Arrays.sort(A); | |
int counter = 1; | |
for(int i = 1; i < A.length; i++) { | |
if(A[i] != A[i - 1]) { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment