Distinct List Coderbyte Solution

0

Have the function DistinctList(arr) take the array of numbers stored in arr and determine the total number of duplicate entries. 

For example, if the input is [1, 2, 2, 2, 3] then your program should output 2 because there are two duplicates of one of the elements.

Distinct List Coderbyte Solution In Java


import java.util.Arrays;
import java.util.Scanner;


public class DistinctList {
int DistinctList(int[] arr) {
Arrays.sort(arr);

int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
count++;
}
}
return count;

}

public static void main (String[] args) {
Scanner s = new Scanner(System.in);
DistinctList c = new DistinctList();
System.out.print(c.DistinctList(new int[]
{0, -1, -1, 2, 5, 2, 3, 5}));
}
}


Distinct List Coderbyte Solution In JavaScript


function DistinctList(arr){
var duplicates=0;
for(var i=0; i<arr.length; i++){
for(var j=0; j<arr.length; j++){
if(arr[i]===arr[j] && i!==j){
duplicates++;
arr.splice(arr.indexOf(arr[i]), 1)
}
}
}
for(var i=0; i<arr.length; i++){
for(var j=0; j<arr.length; j++){
if(arr[i]===arr[j] && i!==j){
duplicates++;
arr.splice(arr.indexOf(arr[i]), 1)
}
}
}
return duplicates;
}



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !