Related
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 8 months ago.
Consider the array [1,2,2]
The array contains two unique values: 1, 2
The array contains duplicate values: 2
The lonely integer is 1
How can the lonely integer be returned?
For an array where you only care about grabbing the first integer which is lonely, you can check if the indexOf and lastIndexOf are the same. If they are, then it's lonely.
const array = [2, 2, 1, 3, 4, 3, 4];
const findLonely = (arr) => {
for (const num of arr) {
if (arr.indexOf(num) === arr.lastIndexOf(num)) return num;
}
return 'No lonely integers.';
};
console.log(findLonely(array));
If you have an array that has multiple lonely values, you can use this method to find all of the lonely values:
const array = [2, 2, 1, 3, 4, 3, 4, 6, 8, 8, 9];
const findAllLonely = (arr) => {
const map = {};
arr.forEach((num) => {
// Keep track of the number of time each number appears in the array
if (!map[num]) return (map[num] = 1);
map[num]++;
});
// Filter through and only keep the values that have 1 instance
return Object.keys(map).filter((key) => {
return map[key] === 1;
});
};
console.log(findAllLonely(array)); // expect [1, 6, 9]
const array = [0,1,2,2,1,5,4,3,4,3,2];
let lonely = array.filter((item,index)=> array.indexOf(item) === array.lastIndexOf(item));
console.log(lonely);
Working Demo :
// Array with duplicates
const arrWithDuplicates = [1, 2, 2];
var result = arrWithDuplicates.sort().filter((x,i,arr) => x !== arr[i+1] && x !== arr[i-1]);
console.log(result); // [1]
For each element your can use .filter() to help count the how many times the element is repeated. Then use .filter() again to return only those elements that appear once.
const nums = [1,2,2,3,4,4,4,5,5,6,7,7,7,8,8];
const singles = nums.filter(
//count how many times each element appears
num => nums.filter(n => n === num)
//return only those with freq. of 1
.length === 1
);
console.log( singles );
//OUTPUT: [ 1, 3, 6 ]
Use a 'for' loop with the filter function to loop through the array and only return the value that appears once
const arr = [0, 1, 2, 2, 1];
let unique;
for(var i = 0; i < arr.length; i++) {
if(a.filter(x => x == arr[i]).length == 1) {
unique = arr[i]
}
}
return unique;
the code below solves the challenge with O(n) complexity
function lonelyinteger(a) {
let result;
a.every((e)=>{
if(a.filter(x=>x==e).length==1) {
result = e;
return false;
}return true;
})
return result;
}
O(n) complexity
function lonelyinteger(a) {
for (let i = 0; i < a.length; i++) {
const count = a.filter((v) => v === a[i]).length;
if (count === 1) {
console.log(a[i]);
return a[i];
}
}
}
If there is multiple unique number in an array = [1,2,3,4,5,3,2,1] here 4 and 5 both are unique ,there is two lonely integer so the output should be like this result = [4,5]. In case of single unique integer we can return the result as result = [3] or result = 3. The below code snippet will solve both the scenario.
const array = [1,2,3,4,5,3,2,1]
let result = []
array.every(e => {
if(array.filter(x => x == e).length == 1) {
result.push(e)
}
return true
})
console.log(result)
Explanation: step by step
Your desire array from where you need to get the lonely integer.
We defined result as an array.
You can use simple for loop or array forEach (learn about forEach).
We are using array filter method (learn about filter) to get our work done. Here array.filter(x => x == e) this will result when the value of e is 1 (first element of the array) then the output will be [1,1].
So for 1 the .length == 1 will return false. This process will continue to get false and the if condition will not get executed until a the 'e' became 4 (4th element of the main array).
When 'e' became 4 then the result of array.filter(x => x == 4) will be [4] so the condition array.filter(x => x == e).length == 1 will be true and the if condition will execute. And inside that we are pushing the value 4 to the result array. You can add a next line return false to stop the execution and you will get only one single lonely integer.
return true is required here only if you're using the every method (learn about array every method)
Play with the code to get better understanding or comment if you've some question about this solution. Please give a up-vote if this answer is helpful.
var arr1=[3,4,5,6,7,1,9];
var arr2=[1,3,4,6,7,5,9];
I want to compare arr2 to arr1. But the methods difference() and intersection() only seem to find if the two arrays have the same elements or not. I want to compare the two arrays spot by spot like arr1[0] to arr2[0], arr1[1] to arr2[1]. And it should show:
intersection: 6,7,9
difference: 1,3,4,5
How can I achieve this?
You can do this in lodash by zipping both arrays, filtering, and than taking the last item of each pair. The comperator for intersection is that the pair is equal. The comperator for difference is that the pair are not equal.
const arr1 = [3,4,5,6,7,1,9];
const arr2 = [1,3,4,6,7,5,9];
const compare = (comperator) => (arr1, arr2) =>
_.zip(arr1, arr2)
.filter(comperator)
.map(_.last);
const eq = _.spread(_.eq);
const intersection = compare(eq);
const difference = compare(_.negate(eq));
console.log('intersection ', intersection(arr1, arr2));
console.log('difference ', difference(arr1, arr2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
You could iterate both arrays in parallel and sort them into two seperate Sets:
function* parallel(a, b) {
for(let i = 0; i < a.length || i < b.length; i++)
yield [ a[i], b[i] ];
}
const intersection = new Set,
difference = new Set;
for(const [a, b] of parallel(arr1, arr2)) {
if(a === b)
intersection.add(a);
else
difference.add(a).add(b);
}
console.log([...intersection], [...difference]);
This also could be solved with reduce:
var arr1 = [3, 4, 5, 6, 7, 1, 9];
var arr2 = [1, 3, 4, 6, 7, 5, 9];
const f = (a, b) => b.reduce((r,c,i) => (a[i] == c ?
r.intersection.push(c) :
r.difference.push(c), r), {intersection: [], difference: []})
console.log(f(arr1, arr2))
Where you start with a pre-set accumulator object and compare each array value using the index.
You could use xor from lodash and it will return an empty array if the arrays have the same elements.
const a1= ['a', 'b', 'd']
const a2= ['d', 'b', 'a']
_.xor(a1, a2).length;//0
Why don't you write your own utility function that checks equality of the sequence? Something like:
export function sequenceEqual<T>(firstSequence: T[], secondSequence: T[]): boolean {
if(!firstSequence || !secondSequence) return false;
return firstSequence.every(
(d, i) => d === secondSequence[i]
);
}
This way you can just return boolean. There is no need to perform an extra step to check if the code returned some array or number or whatever, what is length of the returned type, which number it is etc. You just ask are my sequences equal and get true or false.
One more benefit is that you are not dependent on some library. Unless they have sequenceEqual so that you don't have to write from scratch, just call it, but I couldn't find it yet in Lodash.
I'm actually looking for a way to check if two values of an array or more are equal. Here are some examples :
[1, 2, 3] // false
[1, 1, 5] // true
['a', 'b', 'a', 'c'] // true
[10, 10, 10] // true
I found this function that gives 'true' if EVERY array values are equal, but that's not what i'd like :
[1,1,1,1].every( (val, i, arr) => val === arr[0] ) // true
You can use a Set to eliminate duplicates:
const nonUnique = a => new Set(a).size !== a.length;
console.log(nonUnique([1, 2, 3])); // false
console.log(nonUnique([1, 1, 5])); // true
console.log(nonUnique(['a', 'b', 'a', 'c'])); // true
console.log(nonUnique([10, 10, 10])); // true
One simple way would be create a Set of the unique values and compare size of Set to length of array
const hasDuplicates = (arr) => new Set(arr).size < arr.length ;
console.log(hasDuplicates([1, 2, 3]));
console.log(hasDuplicates([1, 1, 5]));
console.log(hasDuplicates(['a', 'b', 'a', 'c']));
console.log(hasDuplicates([10, 10, 10]));
This algorithm is pretty inefficient (O(n^2), brute force search), but it works:
function has_dupes(arr) {
return arr.some((x, i) => arr.includes(x, i+1));
}
console.log(has_dupes([1, 2, 3]));
console.log(has_dupes([1, 1, 5]));
console.log(has_dupes(['a', 'b', 'a', 'c']));
console.log(has_dupes([10, 10, 10]));
For every element x at some index i, we check whether the subarray starting at i+1 includes another value that's equal to x.
Using a for loop we can break the loop once we find a duplicate.
var a = [1, 2, 3] // false
var b = [1, 1, 5] // true
var c = ['a', 'b', 'a', 'c'] // true
var d = [10, 10, 10] // true
function diff(arr){
var diff = []
for(let i = 0; i<arr.length; i++){
if(diff.includes(arr[i])){
return true;//<-- break the loop
}else{
diff.push(arr[i]);
}
}
return false;
}
console.log(diff(a))
console.log(diff(b))
console.log(diff(c))
console.log(diff(d))
You could use a double for loop where one is the index of what is being looked for and the inner loop checking for a duplicate.
You can find all unique elements through lodash and just compare the size of both the arrays to get what you want.
a = [1, 2, 3]
a_uniq = _.uniq(a); //will return [1, 3]
console.log(a.length != a_uniq.length); //will return false
a = [1, 1, 3]
a_uniq = _.uniq(a); //will return [1, 3]
console.log(a.length != a_uniq.length); //will return true
Can test here quickly: https://codepen.io/travist/full/jrBjBz/
Reduce the array into an object with keys being the items. If the object's keys are a different length than the items length, there are duplicates.
This also gives you an indication of the number of duplicates.
const items1 = [1, 2, 3, 3, 4]
const items2 = [1, 2, 3, 3, 3, 3, 4]
const items3 = [1, 2, 3, 4]
const items4 = [undefined, undefined]
function dupCount(array) {
const obj = array.reduce((acc, item) => {
acc[item] = true;
return acc;
}, {})
return array.length - Object.keys(obj).length
}
console.log(dupCount(items1))
console.log(dupCount(items2))
console.log(dupCount(items3))
console.log(dupCount(items4))
function isSameArray(data, count) {
var result = false;
data.forEach(a => {
const filter = data.filter(f => f === a);
if (filter.length > count - 1) {
result = true;
return;
}
});
return result;
}
isSameArray([1, 2, 3], 2)
Add them to a set while checking using the find method
let items1 = [1, 2, 3, 3, 4];
let items2 = [1, 2, 3, 4];
let items3 = ['a', 'b', 'a', 'c']
let items4 = [0, 0]
let items5 = [undefined, undefined]
function hasDuplicate(array) {
const set = new Set()
return array.some(it => {
if (set.has(it)) {
return true
} else {
set.add(it);
return false;
}
})
}
console.log(hasDuplicate(items1))
console.log(hasDuplicate(items2))
console.log(hasDuplicate(items3))
console.log(hasDuplicate(items4))
console.log(hasDuplicate(items5))
You can loop it easily using for loop for example:-
function hasDuplicate(lst){
for(var i=0; i<lst.length; i++)
for(var j=i+1; j<lst.length;j++)
if(i!=j && lst[i] === lst[j])
return true;
return false;
}
var list1 = [1,1,5];
window.alert(hasDuplicate(list1)); //True
Array.some works the same as Array.every, except if the test passes true for one of the items in the array you're iterating over, execution stops and it returns true. Probably what you're looking for.
The following will help you in achieving the result which u gave as an example:
Array_is_true(int *a,int n)
{
int j=1,flag=0;
for(int i=0,i<n;i++)
{
j=i+1;
while(j<n;)
{
if(a[i]==a[j])
{ flag=1;j++;}
}
}
}
What is the easiest way to remove all elements from array that match specific string? For example:
array = [1,2,'deleted',4,5,'deleted',6,7];
I want to remove all 'deleted' from the array.
Simply use the Array.prototype.filter() function for obtain elements of a condition
var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})
Update: ES6 Syntax
let array = [1,2,'deleted',4,5,'deleted',6,7]
let newarr = array.filter(a => a !== 'deleted')
If you have multiple strings to remove from main array, You can try this
// Your main array
var arr = [ '8','abc','b','c'];
// This array contains strings that needs to be removed from main array
var removeStr = [ 'abc' , '8'];
arr = arr.filter(function(val){
return (removeStr.indexOf(val) == -1 ? true : false)
})
console.log(arr);
// 'arr' Outputs to :
[ 'b', 'c' ]
OR
Better Performance(Using hash) , If strict type equality not required
// Your main array
var arr = [ '8','deleted','b','c'];
// This array contains strings that needs to be removed from main array
var removeStr = [ 'deleted' , '8'];
var removeObj = {}; // Use of hash will boost performance for larger arrays
removeStr.forEach( e => removeObj[e] = true);
var res = arr.filter(function(val){
return !removeObj[val]
})
console.log(res);
// 'arr' Outputs to :
[ 'b', 'c' ]
If you want the same array then you can use
var array = [1,2,'deleted',4,5,'deleted',6,7];
var index = "deleted";
for(var i = array.length - 1; i >= 0; i--) {
if(array[i] === index) {
array.splice(i, 1);
}
}
EXAMPLE 1
else you can use Array.prototype.filter which creates a new array with all elements that pass the test implemented by the provided function.
var arrayVal = [1,2,'deleted',4,5,'deleted',6,7];
function filterVal(value) {
return value !== 'deleted';
}
var filtered = arrayVal.filter(filterVal);
EXAMPLE 2
array = array.filter(function(s) {
return s !== 'deleted';
});
A canonical answer would probably look like this:
[10, 'deleted', 20, 'deleted'].filter(x => x !== 'deleted');
//=> [10, 20]
There's nothing unexpected here; any developers can read, understand and maintain this code. From that perspective this solution is great. I just want to offer some different perspectives.
Firstly I sometimes struggle with the semantic of filter when the condition is "reversed":
[2, 3, 2, 3].filter(x => x === 2);
[2, 3, 2, 3].filter(x => x !== 2);
This is a contrived example but I bet a few readers did pause for a nanosecond. These small cognitive bumps can be exhausting in the long run.
I personally wish there would be a reject method:
[2, 3, 2, 3].filter(x => x === 2);
[2, 3, 2, 3].reject(x => x === 2);
Secondly there's a lot of "machinery" in this expression x => x === 2: a function expression, a parameter and an equality check.
This could be abstracted away by using a curried function:
const eq =
x => y =>
x === y;
[2, 3, 2, 3].filter(eq(2));
//=> [2, 2]
We can see that eq(2) is the same as x => x === 2 just shorter and with added semantic.
Now let's build a reject function and use eq:
const reject =
(pred, xs) =>
xs.filter(x =>
pred(x) === false);
reject(eq(2), [2, 3, 2, 3]);
//=> [3, 3]
But what if we need to reject other things? Well we can build an either function that uses eq:
const either =
(...xs) => y =>
xs.some(eq(y));
reject(either(1, 2), [1, 3, 2, 3]);
//=> [3, 3]
Finally to answer your question:
reject(eq('deleted'), [1, 'deleted', 3]);
//=> [1, 3]
reject(either('deleted', 'removed'), [1, 'deleted', 3, 'removed', 5]);
//=> [1, 3, 5]
We could go further and remove based on different predicates e.g. remove if matches the string "delete" or is 0.
Let's build a eitherfn function that takes a list of predicates:
const eitherfn =
(...fn) => y =>
fn.some(f =>
f(y));
And now let's build a match function:
const match =
x => y =>
typeof y === 'string'
? y.includes(x)
: false;
Then:
reject(eitherfn(match('delete'), eq(0)), [0, 1, 'deleted', 3, 'will delete', 5])
// [1, 3, 5]
Consider the following simple array:
var foods = ['hotdog', 'hamburger', 'soup', 'sandwich', 'hotdog', 'watermelon', 'hotdog'];
With underscore, is there a function or combination of functions I can use to select the most frequently occurring value (in this case it's hotdog)?
var foods = ['hotdog', 'hamburger', 'soup', 'sandwich', 'hotdog', 'watermelon', 'hotdog'];
var result = _.chain(foods).countBy().pairs().max(_.last).head().value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
countBy:
Sorts a list into groups and returns a count for the number of objects
in each group.
pairs:
Convert an object into a list of [key, value] pairs.
max:
Returns the maximum value in list. If an iterator function is provided, it will be used on each value to generate the criterion by which the value is ranked.
last:
Returns the last element of an array
head:
Returns the first element of an array
chain:
Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is used.
value:
Extracts the value of a wrapped object.
You can do this in one pass using _.reduce. The basic idea is to keep track of the word frequencies and the most common word at the same time:
var o = _(foods).reduce(function(o, s) {
o.freq[s] = (o.freq[s] || 0) + 1;
if(!o.freq[o.most] || o.freq[s] > o.freq[o.most])
o.most = s;
return o;
}, { freq: { }, most: '' });
That leaves 'hotdot' in o.most.
Demo: http://jsfiddle.net/ambiguous/G9W4m/
You can also do it with each (or even a simple for loop) if you don't mind predeclaring the cache variable:
var o = { freq: { }, most: '' };
_(foods).each(function(s) {
o.freq[s] = (o.freq[s] || 0) + 1;
if(!o.freq[o.most] || o.freq[s] > o.freq[o.most])
o.most = s;
});
Demo: http://jsfiddle.net/ambiguous/WvXEV/
You could also break o into two pieces and use a slightly modified version of the above, then you wouldn't have to say o.most to get 'hotdog'.
Based off previous answers, here's my version to calculate mode for either an array of strings or numbers that accounts for draws in mode (when values have equal frequency) and also returns the frequency. You can then choose what to do with them.
Underscore and Lodash included.
// returns
// [['item', frequency]] where item is a string and frequency is an interger
// [['item', frequency], ['item', frequency], ['item', frequency] ... ] for draws
// examples:
// unique mode: creed appears the most times in array
// returns: [['creed', 4]]
// draw mode: 'jim' and 'creed' both occur 4 times in array
// returns: [['jim', 4], ['creed', 4]]
// underscore:
const usMode = arr => _.chain(arr).countBy().pairs().value().sort((a, b) => b[1] - a[1]).filter((e,i,a) => e[1] === a[0][1])
// lodash
const ldMode = arr => _.chain(arr).countBy().toPairs().value().sort((a, b) => b[1] - a[1]).filter((e,i,a) => e[1] === a[0][1])
// usage
usMode([1,2,3,3,3,4,5,6,7])
// [['3', 3]]
using underscore:
// underscore
const strsUniqueArr = ['jim','pam','creed','pam','jim','creed','creed','creed']
const strsDrawArr = ['pam','jim','creed','jim','jim','creed','creed','creed', 'pam', 'jim']
const numsUniqueArr = [1, 2, 2, 2, 2, 3 ,4, 5]
const numsDrawArr = [1, 1, 1, 1, 2, 2, 2, 2, 3 ,4, 5]
const usMode = arr => _.chain(arr).countBy().pairs().value().sort((a, b) => b[1] - a[1]).filter((e,i,a) => e[1] === a[0][1])
console.log('empty', usMode([]))
console.log('unique strs', usMode(strsUniqueArr))
console.log('draw sts', usMode(strsDrawArr))
console.log('unique nums', usMode(numsUniqueArr))
console.log('draw nums', usMode(numsDrawArr))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
using lodash:
// lodash
const strsUniqueArr = ['jim','pam','creed','pam','jim','creed','creed','creed']
const strsDrawArr = ['pam','jim','creed','jim','jim','creed','creed','creed', 'pam', 'jim']
const numsUniqueArr = [1, 2, 2, 2, 2, 3 ,4, 5]
const numsDrawArr = [1, 1, 1, 1, 2, 2, 2, 2, 3 ,4, 5]
const ldMode = arr => _.chain(arr).countBy().toPairs().value().sort((a, b) => b[1] - a[1]).filter((e,i,a) => e[1] === a[0][1])
console.log('empty', ldMode([]))
console.log('unique strs', ldMode(strsUniqueArr))
console.log('draw sts', ldMode(strsDrawArr))
console.log('unique nums', ldMode(numsUniqueArr))
console.log('draw nums', ldMode(numsDrawArr))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>