1

I need to print the duplicates from an array.

For example, given the following array and index, the function will print 6,23,33,100.

var array = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100];

Apparently we need to do it using two 'while loops'.

Im looking for a solution, but more importantly an explanation. Links are very much appreciated.

Thanks!

2
  • Do you have to use "two while loops"? Otherwise the answer from @Dince12 does the job well. Commented Jan 6, 2020 at 12:33
  • Yes, for practice we should use a loop within a loop (is it called Nested loops (?)) Commented Jan 6, 2020 at 13:21

4 Answers 4

2

The most elegant and efficient solution is to use a while loop which iterates the array only once, so, we have O(N) complexity.

For this, we need to declare an hash which keeps the number of occurencies for each array's item. If we found a duplicate one then we store it in duplicates array.

var arr = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100], i = 0, hash = {}, duplicates = [];
while(i < arr.length){
  hash[arr[i]] = hash[arr[i]] ? hash[arr[i]] += 1 : 1;
  if (hash[arr[i]] === 2) 
    duplicates.push(arr[i])
  i++;
}

console.log(duplicates)

Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot Michai, works really good! what should I search for to find explanation for this method?
You need to read about arrays methods in javascript, especially.
my mentor just told me I need to do it with the 'even-if' function, instead of 'hash[arr[i]] = hash[arr[i]] ? hash[arr[i]] += 1 : 1;'
The you can use if(!hash[arr[i]) hash[arr[i]] = 1 else hash[arr[i]]++
1

You could use the filter() and indexOf() methods.

var array = [3,6,67,6,23,11,100,8,93,0,17,24,7,1,33,45,28,33,23,12,99,100];
console.log(array.filter((a,b,c) => c.indexOf(a) !== b));

a -> is the value being passed in the arrow function.

b -> is the index being passed in.

c -> is the whole array being passed in.

This line is filtering the array based on, if the original array has a value (argument a) whose index does not match the given index passed in through the arrow function (argument b).

3 Comments

OP is probably looking for something much more basic. They are a beginner and according to the task, the solution is intended to use regular loops.
@VLAZ How is this more complex than any other answers so far (mine included) ;) ?
@customcommander we're talking about somebody who is trying to learn loops. The iteration methods are a generalisation of loop logic and thus a concept that should come after. We don't start doing algebra in school straight from multiplication by skipping addition.
0

A good sample and explanation can be found here... W3Resource

Futhermore to assist in understanding the two major components of the code the Object... Working with objects - Javascipt

and Arrays...Javascipt Arrays

Comments

0

For the shortest approach, you could take a closure

(s =>                           )(       )

with a Set

(s =>                           )(new Set)

and a check. If a value is already seen, then take this value

(s => v => s.has(v)             )(new Set)

or add the value to the set and return false, because a not seen value should not in the result set.

(s => v =>             !s.add(v))(new Set)

var array = [3, 6, 67, 6, 23, 11, 100, 8, 93, 0, 17, 24, 7, 1, 33, 45, 28, 33, 23, 12, 99, 100],
    duplicates = array.filter((s => v => s.has(v) || !s.add(v))(new Set));

console.log(duplicates);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.