Let's see we have an array with unknown number, type, and value of elements. For example, we have an array and the expected result will be that it contains 200 "a" element, 300 "b" element, and 500 "c" element. But again, we don't know the value, number, place of elements. What is the easiest way to get them like the result above?
Asked
Active
Viewed 96 times
0
-
can you explain abit more what you want, do you want to remove duplicates from an array? or try find an element in an array? could you give us a better example of what you're looking for. – Joe Warner Oct 23 '18 at 11:00
-
First, the "a" is an example. it can be 1, 5, 100, or anything. Secondly, yes, it's a possible solution to first remove the duplicates, then count how many different elements we have, but if we have an array with 10000 elements, looping through it twice can be a bit slow. So is there any solution which can count the different elements? – Gergő Horváth Oct 23 '18 at 11:00
-
so you want the number of unquie items? not the items themselves you just want a number of unquie items? – Joe Warner Oct 23 '18 at 11:01
-
@JoeWarner `const arr = [1, 1, 2, 2, 3, 1]` expected: `{1: 3, 2: 2, 3: 1}`. Imagine with thousands of elements – Gergő Horváth Oct 23 '18 at 11:01
-
oh that wasnt very clear i cant answer now as its been marked duplicate you can do this fairly easy with reduce – Joe Warner Oct 23 '18 at 11:02
1 Answers
0
Please remove the duplicity and find the unique elements of array by using simple while loop in the easiest way.
let alphaNumeric = ["a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","c","c","c","c","c","c","c","c","1","1","1","1","1","a", "d","#","!", "p", "d", "d", "d", "d", "d"];
let i = 0;
let length = alphaNumeric.length;
let removeDuplicate = [];
while(i < length) {
if (!removeDuplicate.includes(alphaNumeric[i])){
removeDuplicate.push(alphaNumeric[i]);
}
i += 1;
}
console.log(removeDuplicate);
Hope, it helps.

Sami Ahmed Siddiqui
- 2,328
- 1
- 16
- 29