-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportant.js
More file actions
47 lines (36 loc) · 1.07 KB
/
important.js
File metadata and controls
47 lines (36 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var fruits = ['apple', 'banana', 'orange', 'kiwi', 'pineapple', 'orange', 'banana', 'apple', 'banana', 'orange', 'grapes', 'papaya', 'grapes'];
//Q.no.1)find unique array of elements from ablve fruits
// function unique(arr) {
// var new_arr = [];
// for (i in fruits) {
// if (new_arr.indexOf(fruits[i]) === -1) {
// new_arr.push(fruits[i]);
// }
// }
// console.log(new_arr);
// };
// function unique(arr) {
// var new_arr = [];
// fruits.forEach((fruit, index, arr) => {
// if (new_arr.indexOf(fruit) === -1) {
// new_arr.push(fruit);
// }
// });
// console.log(new_arr)
// }
// unique(fruits);
//Q.no.2) Find the count of items in the fruit array
function count(arr) {
var obj = {};
arr.forEach(fruit => {
// if (obj[fruit]) {
// obj[fruit] = obj[fruit] + 1;
// } else {
// obj[fruit] = 1;
// }
obj[fruit] = obj[fruit] ? obj[fruit] + 1 : 1;
//obj[fruit] = (obj[fruit] || 0) + 1;
})
console.log(obj)
}
count(fruits);