I was asked an question for writing a program where the input will be array of numbers;
the output of the program will be array of numbers without duplicates.
I coded in javaScript which runs on node, as follows.
In my first attempt, I first sorted the numbers in ascending order and then I removed the duplicates by comparing the number from previously occurred numbers
the code is as follows,
var numList = []; // below function is used to capture the // command-line parameters for array and the // number to be searched (function(){ process.argv.forEach(function (val, index, array) { var idx = 0, ar = undefined; try{ if(index === 2){ ar = val.split(","); // convert the numbers present as string values is array // to array of numbers for(idx = 0; idx < ar.length; idx++){ numList.push(parseInt(ar[idx])); } } }catch(e){ console.log(e) } }); })(); console.log(" Input Array ",numList); numList = uniqueNumArray01(numList); console.log(" Array without duplicate numbers ",numList) function uniqueNumArray01(numberList){ var _numberList = [], nextIndex = 0; currentIndex = 0, prevIndex = 0, temp = 0, isSwapped = false, index = 0; // do bubble sort on input array of numbers do{ isSwapped = false; for(index = 0; index < numberList.length; index++){ currentIndex = index; nextIndex = index + 1; if(nextIndex >= numberList.length){ break; } if(numberList[currentIndex] > numberList[nextIndex]){ temp = numberList[currentIndex]; numberList[currentIndex] = numberList[nextIndex]; numberList[nextIndex] = temp; isSwapped = true; } }// end of for }while(isSwapped === true); console.log("Sorted Number array ",numberList); // create array of unique numbers for(index = 0; index < numberList.length; index++){ if(index === 0){ currentIndex = index; prevIndex = index; _numberList.push(numberList[currentIndex]); }else{ currentIndex = index; if(numberList[currentIndex] !== numberList[prevIndex]){ _numberList.push(numberList[currentIndex]); } prevIndex = currentIndex; } // end of else } // end of for return _numberList; } // en of uniqueNumArray01
The output of the above code is as follows,
E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript>node duplicateNum01.js 12,34,12,56,12,34,56,1,2,12,12,12 Input Array [ 12, 34, 12, 56, 12, 34, 56, 1, 2, 12, 12, 12 ] Sorted Number array [ 1, 2, 12, 12, 12, 12, 12, 12, 34, 34, 56, 56 ] Array without duplicate numbers [ 1, 2, 12, 34, 56 ]
He was not satisfied;
So I suggested an another solution where I wont sort the array, instead I will first iterate through the array and on each iteration I will check if the number present in current iteration is present in previous positions.
Hence to check if the current number ever exists in previous positions, I will again start from start index and do comparison; this I will do till the previous index of the current one.
My next solution is as follows,
console.log(" Input Array ",numList); numList = uniqueNumArray02(numList); console.log(" Array without duplicate numbers ",numList); function uniqueNumArray02(numberList){ var index = 0, _numberList = [],// new array holding non repeating numbers startIndex = 0, lastIndex = 0, currentNumber = 0, prevNumber = 0, isDuplicateFound = false; for(index = 0; index < numberList.length; index++){ currentNumber = numberList[index]; startIndex = 0; isDuplicateFound = false; if(index === 0){ _numberList.push(currentNumber); }else{ // search for occurance of number in previous part of array // till the current index for(startIndex = 0; startIndex < index; startIndex++){ prevNumber = numberList[startIndex]; if(prevNumber === currentNumber){ isDuplicateFound = true; break; } } // end of for if(isDuplicateFound === false){ _numberList.push(currentNumber); } }// end of else }// end of for return _numberList; }// end of uniqueNumArray02
The output of the above program is as follows,
E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript>node duplicateNum01.js 12,34,12,56 Input Array [ 12, 34, 12, 56 ] Array without duplicate numbers [ 12, 34, 56 ] E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript>node duplicateNum01.js 12,34,12,56,12,34,56,1,2,12,12,12 Input Array [ 12, 34, 12, 56, 12, 34, 56, 1, 2, 12, 12, 12 ] Array without duplicate numbers [ 12, 34, 56, 1, 2 ]
But the interviewer was not satisfied; but appreciated my application of logic.
Is there a better solution for the above problem.
Requesting to please do a code review and also suggest if I can do better here, please guide me, waiting for valuable reply.
Array.from(new Set(numList))
\$\endgroup\$