xxxxxxxxxx
var input = [1, 2, 5, 5, 3, 1, 3, 1];
var duplicates = input.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);
console.log(duplicates);
xxxxxxxxxx
const myArray = ['a', 'b', 'c', 'c', 'b', 'd'];
const elementCounts = {};
myArray.forEach(element => {
elementCounts[element] = (elementCounts[element] || 0) + 1;
});
console.log(elementCounts);
//output
/*{
a: 1,
b: 2,
c: 2,
d: 1
}*/
xxxxxxxxxx
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];
const set = new Set(numbers);
const duplicates = numbers.filter(item => {
if (set.has(item)) {
set.delete(item);
} else {
return item;
}
});
console.log(duplicates);
// [ 2, 5 ]
xxxxxxxxxx
function findDuplicates(arr) {
const duplicates = new Set()
return arr.filter(item => {
if (duplicates.has(item)) {
return true
}
duplicates.add(item)
return false
})
}
xxxxxxxxxx
const order = ["apple", "banana", "orange", "banana", "apple", "banana"];
const result = order.reduce(function (prevVal, item) {
if (!prevVal[item]) {
// if an object doesn't have a key yet, it means it wasn't repeated before
prevVal[item] = 1;
} else {
// increase the number of repetitions by 1
prevVal[item] += 1;
}
// and return the changed object
return prevVal;
}, {}); // The initial value is an empty object.
console.log(result); // { apple: 2, banana: 3, orange: 1 }
xxxxxxxxxx
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];
let isDuplicate = false;
// Outer for loop
for (let i = 0; i < numbers.length; i++) {
// Inner for loop
for (let j = 0; j < numbers.length; j++) {
// Skip self comparison
if (i !== j) {
// Check for duplicate
if (numbers[i] === numbers[j]) {
isDuplicate = true;
// Terminate inner loop
break;
}
}
// Terminate outer loop
if (isDuplicate) {
break;
}
}
}
if (!isDuplicate) {
console.log(`Array doesn't contain duplicates.`);
} else {
console.log(`Array contains duplicates.`);
}
// Output: Array contains duplicates.
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
int[] arr1 = {1,2,3,3,4};
for (int i = 0; i < arr1.length-1 ; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i]==arr1[j] && (j != i)){
System.out.println("array's duplicate number: " +
arr1[j]);
}
}
}
}
}
xxxxxxxxxx
def FirstOccurrence ( array, n ) :
beg = 0
end = len(array) - 1
while (beg <= end) :
mid = int (beg + (end-beg)/2)
if (array[mid] == n) :
if (mid-1 >= 0 and array[mid-1] == n) :
end = mid-1
continue
return mid
elif (array[mid] < n) :
beg = mid + 1
else :
end = mid - 1
return -1
def LastOccurrence (array, n) :
beg = 0
end = len(array)-1
while (beg <= end) :
mid = int(beg + (end-beg)/2)
if (array[mid] == n) :
if (mid+1 < len(array) and array[mid+1] == n) :
beg = mid + 1
continue
return mid
elif (array[mid] < n) :
beg = mid + 1
else :
end = mid - 1
return -1;
array = [ 1, 2, 3, 9, 9, 9, 9, 10, 10, 12, 13 ]
n = int (input("Enter the number : "))
first_index = FirstOccurrence (array, n)
last_index = LastOccurrence (array, n)
if (first_index == -1 or last_index == - 1) :
print("Element does not exist")
else :
print("First occurrence of " + str(n) + " is at index : " + str(first_index))
print("Last occurrence of " + str(n) + " is at index : " + str(last_index))
print("Total count : "+ str(last_index - first_index + 1))