Skip to main content

Posts

Showing posts from April, 2015

Python: Removing Same Elements in a List Without Sorting It

This has the exact same idea with the earlier JavaScript post with almost the same title. In this, I "translated" the JavaScript language into Python language. Python #result flag def check(b, f): for i, val_ref in enumerate(b): for j, val in enumerate(b): if (i Added shorter version April 22, 2015 Shorter version def r_d(a): b = a[0:len(a)] #copy array buffer = [] dummy = "deleted" #dummy string for i, val_ref in enumerate(b): for j, val in enumerate(b): if (i #1 Result in Python's command terminal (1 st version) #2 Result in Python's command terminal (shorter version) This will leave the original array (list) intact, no changes.

JavaScript: Removing Same Elements in an Array without Sorting It

Basic algorithm For instance we have an object array: [10, 1, 1, 2, 1, 2] . Then we want to remove all the same occurrences to be just [10, 1, 2] without changing the original sequence. Here's what I did. First of all, I used my fingers to imagine it. Then, I "translated" it to JavaScript. About the comparing (pairing), it uses combinatoric pattern. If you have 3 fruits and you wanna compare each of them, then you'll need 3 steps. If you have five fingers, then you'll need 10 steps to compare them all. Combination(total_number, 2) That's the basic idea. In this "translation", for instance that 3 fruits example, I created object of pairs. But not actually creating an object, just imaginary. As such: fruit 1 compared to fruit 1, fruit 2, and fruit 3. fruit 2 compared to fruit 1, fruit 2, and fruit 3. fruit 3 compared to fruit 1, fruit 2, and fruit 3. As you can see, there are repetitions. Without conditional statement , it will compa...